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/>.
23 ;; This library implements a LaTeX back-end for Org generic exporter.
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".
38 (eval-when-compile (require 'cl
))
40 (defvar org-export-latex-default-packages-alist
)
41 (defvar org-export-latex-packages-alist
)
43 (declare-function org-element-property
"org-element" (property element
))
44 (declare-function org-element-normalize-string
"org-element" (s))
45 (declare-function org-element-parse-secondary-string
46 "org-element" (string restriction
&optional buffer
))
47 (defvar org-element-string-restrictions
)
48 (defvar org-element-object-restrictions
)
50 (declare-function org-export-data
"org-export" (data info
))
51 (declare-function org-export-directory
"org-export" (type plist
))
52 (declare-function org-export-expand-macro
"org-export" (macro info
))
53 (declare-function org-export-first-sibling-p
"org-export" (headline info
))
54 (declare-function org-export-footnote-first-reference-p
"org-export"
55 (footnote-reference info
))
56 (declare-function org-export-format-code
"org-export"
57 (code fun
&optional num-lines ref-alist
))
58 (declare-function org-export-format-code-default
"org-export" (element info
))
59 (declare-function org-export-get-coderef-format
"org-export" (path desc
))
60 (declare-function org-export-get-footnote-definition
"org-export"
61 (footnote-reference info
))
62 (declare-function org-export-get-footnote-number
"org-export" (footnote info
))
63 (declare-function org-export-get-previous-element
"org-export" (blob info
))
64 (declare-function org-export-get-relative-level
"org-export" (headline info
))
65 (declare-function org-export-unravel-code
"org-export" (element))
66 (declare-function org-export-inline-image-p
"org-export"
67 (link &optional extensions
))
68 (declare-function org-export-last-sibling-p
"org-export" (headline info
))
69 (declare-function org-export-low-level-p
"org-export" (headline info
))
70 (declare-function org-export-output-file-name
71 "org-export" (extension &optional subtreep pub-dir
))
72 (declare-function org-export-resolve-coderef
"org-export" (ref info
))
73 (declare-function org-export-resolve-fuzzy-link
"org-export" (link info
))
74 (declare-function org-export-secondary-string
"org-export" (secondary info
))
75 (declare-function org-export-solidify-link-text
"org-export" (s))
77 org-export-to-buffer
"org-export"
78 (backend buffer
&optional subtreep visible-only body-only ext-plist
))
80 org-export-to-file
"org-export"
81 (backend file
&optional subtreep visible-only body-only ext-plist
))
85 ;;; Internal Variables
87 (defconst org-e-latex-option-alist
88 '((:date
"DATE" nil org-e-latex-date-format t
)
89 (:latex-class
"LATEX_CLASS" nil org-e-latex-default-class t
)
90 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
91 (:latex-header-extra
"LATEX_HEADER" nil nil newline
))
92 "Alist between LaTeX export properties and ways to set them.
93 See `org-export-option-alist' for more information on the
94 structure of the value.")
98 ;;; User Configurable Variables
100 (defgroup org-export-e-latex nil
101 "Options for exporting Org mode files to LaTeX."
102 :tag
"Org Export LaTeX"
108 (defcustom org-e-latex-default-class
"article"
109 "The default LaTeX class."
110 :group
'org-export-e-latex
111 :type
'(string :tag
"LaTeX class"))
113 (defcustom org-e-latex-classes
115 "\\documentclass[11pt]{article}"
116 ("\\section{%s}" .
"\\section*{%s}")
117 ("\\subsection{%s}" .
"\\subsection*{%s}")
118 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
119 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
120 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
122 "\\documentclass[11pt]{report}"
123 ("\\part{%s}" .
"\\part*{%s}")
124 ("\\chapter{%s}" .
"\\chapter*{%s}")
125 ("\\section{%s}" .
"\\section*{%s}")
126 ("\\subsection{%s}" .
"\\subsection*{%s}")
127 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
129 "\\documentclass[11pt]{book}"
130 ("\\part{%s}" .
"\\part*{%s}")
131 ("\\chapter{%s}" .
"\\chapter*{%s}")
132 ("\\section{%s}" .
"\\section*{%s}")
133 ("\\subsection{%s}" .
"\\subsection*{%s}")
134 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
135 "Alist of LaTeX classes and associated header and structure.
136 If #+LaTeX_CLASS is set in the buffer, use its value and the
137 associated information. Here is the structure of each cell:
141 \(numbered-section . unnumbered-section\)
147 The HEADER-STRING is the header that will be inserted into the
148 LaTeX file. It should contain the \\documentclass macro, and
149 anything else that is needed for this setup. To this header, the
150 following commands will be added:
152 - Calls to \\usepackage for all packages mentioned in the
153 variables `org-export-latex-default-packages-alist' and
154 `org-export-latex-packages-alist'. Thus, your header
155 definitions should avoid to also request these packages.
157 - Lines specified via \"#+LaTeX_HEADER:\"
159 If you need more control about the sequence in which the header
160 is built up, or if you want to exclude one of these building
161 blocks for a particular class, you can use the following
162 macro-like placeholders.
164 [DEFAULT-PACKAGES] \\usepackage statements for default packages
165 [NO-DEFAULT-PACKAGES] do not include any of the default packages
166 [PACKAGES] \\usepackage statements for packages
167 [NO-PACKAGES] do not include the packages
168 [EXTRA] the stuff from #+LaTeX_HEADER
169 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
170 [BEAMER-HEADER-EXTRA] the beamer extra headers
174 \\documentclass{article}
175 [NO-DEFAULT-PACKAGES]
177 \\providecommand{\\alert}[1]{\\textbf{#1}}
180 will omit the default packages, and will include the
181 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
182 then place \\usepackage commands based on the content of
183 `org-export-latex-packages-alist'.
185 If your header, `org-export-latex-default-packages-alist' or
186 `org-export-latex-packages-alist' inserts
187 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
188 replaced with a coding system derived from
189 `buffer-file-coding-system'. See also the variable
190 `org-e-latex-inputenc-alist' for a way to influence this
193 The sectioning structure
194 ------------------------
196 The sectioning structure of the class is given by the elements
197 following the header string. For each sectioning level, a number
198 of strings is specified. A %s formatter is mandatory in each
199 section string and will be replaced by the title of the section.
201 Instead of a cons cell \(numbered . unnumbered\), you can also
202 provide a list of 2 or 4 elements,
204 \(numbered-open numbered-close\)
208 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
210 providing opening and closing strings for a LaTeX environment
211 that should represent the document section. The opening clause
212 should have a %s to represent the section title.
214 Instead of a list of sectioning commands, you can also specify
215 a function name. That function will be called with two
216 parameters, the \(reduced) level of the headline, and a predicate
217 non-nil when the headline should be numbered. It must return
218 a format string in which the section title will be added."
219 :group
'org-export-e-latex
221 (list (string :tag
"LaTeX class")
222 (string :tag
"LaTeX header")
223 (repeat :tag
"Levels" :inline t
226 (string :tag
" numbered")
227 (string :tag
"unnumbered"))
228 (list :tag
"Environment"
229 (string :tag
"Opening (numbered)")
230 (string :tag
"Closing (numbered)")
231 (string :tag
"Opening (unnumbered)")
232 (string :tag
"Closing (unnumbered)"))
233 (function :tag
"Hook computing sectioning"))))))
235 (defcustom org-e-latex-inputenc-alist nil
236 "Alist of inputenc coding system names, and what should really be used.
237 For example, adding an entry
239 (\"utf8\" . \"utf8x\")
241 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
242 are written as utf8 files."
243 :group
'org-export-e-latex
246 (string :tag
"Derived from buffer")
247 (string :tag
"Use this instead"))))
249 (defcustom org-e-latex-date-format
251 "Format string for \\date{...}."
252 :group
'org-export-e-latex
255 (defcustom org-e-latex-title-command
"\\maketitle"
256 "The command used to insert the title just after \\begin{document}.
257 If this string contains the formatting specification \"%s\" then
258 it will be used as a formatting string, passing the title as an
260 :group
'org-export-e-latex
266 (defcustom org-e-latex-format-headline-function nil
267 "Function to format headline text.
269 This function will be called with 5 arguments:
270 TODO the todo keyword \(string or nil\).
271 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
272 PRIORITY the priority of the headline \(integer or nil\)
273 TEXT the main headline text \(string\).
274 TAGS the tags string, separated with colons \(string or nil\).
276 The function result will be used in the section format string.
278 As an example, one could set the variable to the following, in
279 order to reproduce the default set-up:
281 \(defun org-e-latex-format-headline \(todo todo-type priority text tags\)
282 \"Default format function for an headline.\"
284 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
286 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
288 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
289 :group
'org-export-e-latex
295 (defcustom org-e-latex-footnote-separator
"\\textsuperscript{,}\\,"
296 "Text used to separate footnotes."
297 :group
'org-export-e-latex
303 (defcustom org-e-latex-active-timestamp-format
"\\textit{%s}"
304 "A printf format string to be applied to active time-stamps."
305 :group
'org-export-e-latex
308 (defcustom org-e-latex-inactive-timestamp-format
"\\textit{%s}"
309 "A printf format string to be applied to inactive time-stamps."
310 :group
'org-export-e-latex
313 (defcustom org-e-latex-diary-timestamp-format
"\\textit{%s}"
314 "A printf format string to be applied to diary time-stamps."
315 :group
'org-export-e-latex
321 (defcustom org-e-latex-image-default-option
"width=.9\\linewidth"
322 "Default option for images."
323 :group
'org-export-e-latex
326 (defcustom org-e-latex-default-figure-position
"htb"
327 "Default position for latex figures."
328 :group
'org-export-e-latex
331 (defcustom org-e-latex-inline-image-rules
332 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
333 "Rules characterizing image files that can be inlined into LaTeX.
335 A rule consists in an association whose key is the type of link
336 to consider, and value is a regexp that will be matched against
339 Note that, by default, the image extension *actually* allowed
340 depend on the way the LaTeX file is processed. When used with
341 pdflatex, pdf, jpg and png images are OK. When processing
342 through dvi to Postscript, only ps and eps are allowed. The
343 default we use here encompasses both."
344 :group
'org-export-e-latex
345 :type
'(alist :key-type
(string :tag
"Type")
346 :value-type
(regexp :tag
"Path")))
351 (defcustom org-e-latex-default-table-environment
"tabular"
352 "Default environment used to build tables."
353 :group
'org-export-e-latex
356 (defcustom org-e-latex-tables-centered t
357 "When non-nil, tables are exported in a center environment."
358 :group
'org-export-e-latex
361 (defcustom org-e-latex-tables-verbatim nil
362 "When non-nil, tables are exported verbatim."
363 :group
'org-export-e-latex
366 (defcustom org-e-latex-tables-booktabs nil
367 "When non-nil, display tables in a formal \"booktabs\" style.
368 This option assumes that the \"booktabs\" package is properly
369 loaded in the header of the document. This value can be ignored
370 locally with \"booktabs=yes\" and \"booktabs=no\" LaTeX
372 :group
'org-export-e-latex
375 (defcustom org-e-latex-table-caption-above t
376 "When non-nil, place caption string at the beginning of the table.
377 Otherwise, place it near the end."
378 :group
'org-export-e-latex
381 (defcustom org-e-latex-table-scientific-notation
"%s\\,(%s)"
382 "Format string to display numbers in scientific notation.
383 The format should have \"%s\" twice, for mantissa and exponent
384 \(i.e. \"%s\\\\times10^{%s}\").
386 When nil, no transformation is made."
387 :group
'org-export-e-latex
389 (string :tag
"Format string")
390 (const :tag
"No formatting")))
395 (defcustom org-e-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
397 (italic .
"\\emph{%s}")
398 (strike-through .
"\\st{%s}")
399 (underline .
"\\underline{%s}")
400 (verbatim . protectedtexttt
))
401 "Alist of LaTeX expressions to convert text markup.
403 The key must be a symbol among `bold', `code', `italic',
404 `strike-through', `underline' and `verbatim'. The value is
405 a formatting string to wrap fontified text with.
407 Value can also be set to the following symbols: `verb' and
408 `protectedtexttt'. For the former, Org will use \"\\verb\" to
409 create a format string and select a delimiter character that
410 isn't in the string. For the latter, Org will use \"\\texttt\"
411 to typeset and try to protect special characters.
413 If no association can be found for a given markup, text will be
415 :group
'org-export-e-latex
417 :options
'(bold code italic strike-through underline verbatim
))
422 (defcustom org-e-latex-format-drawer-function nil
423 "Function called to format a drawer in LaTeX code.
425 The function must accept two parameters:
426 NAME the drawer name, like \"LOGBOOK\"
427 CONTENTS the contents of the drawer.
429 The function should return the string to be exported.
431 For example, the variable could be set to the following function
432 in order to mimic default behaviour:
434 \(defun org-e-latex-format-drawer-default \(name contents\)
435 \"Format a drawer element for LaTeX export.\"
437 :group
'org-export-e-latex
443 (defcustom org-e-latex-format-inlinetask-function nil
444 "Function called to format an inlinetask in LaTeX code.
446 The function must accept six parameters:
447 TODO the todo keyword, as a string
448 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
449 PRIORITY the inlinetask priority, as a string
450 NAME the inlinetask name, as a string.
451 TAGS the inlinetask tags, as a string.
452 CONTENTS the contents of the inlinetask, as a string.
454 The function should return the string to be exported.
456 For example, the variable could be set to the following function
457 in order to mimic default behaviour:
459 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
460 \"Format an inline task element for LaTeX export.\"
464 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
465 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
467 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
468 \(format \(concat \"\\\\begin{center}\\n\"
470 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
472 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
474 \"\\\\end{minipage}}\"
475 \"\\\\end{center}\"\)
476 full-title contents\)\)"
477 :group
'org-export-e-latex
483 (defcustom org-e-latex-listings nil
484 "Non-nil means export source code using the listings package.
485 This package will fontify source code, possibly even with color.
486 If you want to use this, you also need to make LaTeX use the
487 listings package, and if you want to have color, the color
488 package. Just add these to `org-export-latex-packages-alist',
489 for example using customize, or with something like:
491 \(require 'org-e-latex)
492 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"listings\"))
493 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"color\"))
497 \(setq org-e-latex-listings 'minted)
499 causes source code to be exported using the minted package as
500 opposed to listings. If you want to use minted, you need to add
501 the minted package to `org-export-latex-packages-alist', for
502 example using customize, or with
504 \(require 'org-e-latex)
505 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"minted\"))
507 In addition, it is necessary to install pygments
508 \(http://pygments.org), and to configure the variable
509 `org-e-latex-pdf-process' so that the -shell-escape option is
511 :group
'org-export-e-latex
513 (const :tag
"Use listings" t
)
514 (const :tag
"Use minted" 'minted
)
515 (const :tag
"Export verbatim" nil
)))
517 (defcustom org-e-latex-listings-langs
518 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
521 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
522 (html "HTML") (xml "XML")
523 (tex "TeX") (latex "TeX")
524 (shell-script "bash")
526 (ocaml "Caml") (caml "Caml")
527 (sql "SQL") (sqlite "sql"))
528 "Alist mapping languages to their listing language counterpart.
529 The key is a symbol, the major mode symbol without the \"-mode\".
530 The value is the string that should be inserted as the language
531 parameter for the listings package. If the mode name and the
532 listings name are the same, the language does not need an entry
533 in this list - but it does not hurt if it is present."
534 :group
'org-export-e-latex
537 (symbol :tag
"Major mode ")
538 (string :tag
"Listings language"))))
540 (defcustom org-e-latex-listings-options nil
541 "Association list of options for the latex listings package.
543 These options are supplied as a comma-separated list to the
544 \\lstset command. Each element of the association list should be
545 a list containing two strings: the name of the option, and the
548 (setq org-e-latex-listings-options
549 '((\"basicstyle\" \"\\small\")
550 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
552 will typeset the code in a small size font with underlined, bold
555 Note that the same options will be applied to blocks of all
557 :group
'org-export-e-latex
560 (string :tag
"Listings option name ")
561 (string :tag
"Listings option value"))))
563 (defcustom org-e-latex-minted-langs
564 '((emacs-lisp "common-lisp")
567 (shell-script "bash")
569 "Alist mapping languages to their minted language counterpart.
570 The key is a symbol, the major mode symbol without the \"-mode\".
571 The value is the string that should be inserted as the language
572 parameter for the minted package. If the mode name and the
573 listings name are the same, the language does not need an entry
574 in this list - but it does not hurt if it is present.
576 Note that minted uses all lower case for language identifiers,
577 and that the full list of language identifiers can be obtained
580 pygmentize -L lexers"
581 :group
'org-export-e-latex
584 (symbol :tag
"Major mode ")
585 (string :tag
"Minted language"))))
587 (defcustom org-e-latex-minted-options nil
588 "Association list of options for the latex minted package.
590 These options are supplied within square brackets in
591 \\begin{minted} environments. Each element of the alist should
592 be a list containing two strings: the name of the option, and the
595 \(setq org-e-latex-minted-options
596 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
598 will result in src blocks being exported with
600 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
602 as the start of the minted environment. Note that the same
603 options will be applied to blocks of all languages."
604 :group
'org-export-e-latex
607 (string :tag
"Minted option name ")
608 (string :tag
"Minted option value"))))
610 (defvar org-e-latex-custom-lang-environments nil
611 "Alist mapping languages to language-specific LaTeX environments.
613 It is used during export of src blocks by the listings and minted
614 latex packages. For example,
616 \(setq org-e-latex-custom-lang-environments
617 '\(\(python \"pythoncode\"\)\)\)
619 would have the effect that if org encounters begin_src python
620 during latex export it will output
629 (defcustom org-e-latex-quotes
631 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"«~")
632 ("\\(\\S-\\)\"" .
"~»")
633 ("\\(\\s-\\|(\\|^\\)'" .
"'"))
635 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"``")
636 ("\\(\\S-\\)\"" .
"''")
637 ("\\(\\s-\\|(\\|^\\)'" .
"`")))
638 "Alist for quotes to use when converting english double-quotes.
640 The CAR of each item in this alist is the language code.
641 The CDR of each item in this alist is a list of three CONS:
642 - the first CONS defines the opening quote;
643 - the second CONS defines the closing quote;
644 - the last CONS defines single quotes.
646 For each item in a CONS, the first string is a regexp
647 for allowed characters before/after the quote, the second
648 string defines the replacement string for this quote."
649 :group
'org-export-e-latex
651 (cons :tag
"Opening quote"
652 (string :tag
"Regexp for char before")
653 (string :tag
"Replacement quote "))
654 (cons :tag
"Closing quote"
655 (string :tag
"Regexp for char after ")
656 (string :tag
"Replacement quote "))
657 (cons :tag
"Single quote"
658 (string :tag
"Regexp for char before")
659 (string :tag
"Replacement quote "))))
664 (defcustom org-e-latex-pdf-process
665 '("pdflatex -interaction nonstopmode -output-directory %o %f"
666 "pdflatex -interaction nonstopmode -output-directory %o %f"
667 "pdflatex -interaction nonstopmode -output-directory %o %f")
668 "Commands to process a LaTeX file to a PDF file.
669 This is a list of strings, each of them will be given to the
670 shell as a command. %f in the command will be replaced by the
671 full file name, %b by the file base name \(i.e. without
672 extension) and %o by the base directory of the file.
674 The reason why this is a list is that it usually takes several
675 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
676 does not have a clever mechanism to detect which of these
677 commands have to be run to get to a stable result, and it also
678 does not do any error checking.
680 By default, Org uses 3 runs of `pdflatex' to do the processing.
681 If you have texi2dvi on your system and if that does not cause
682 the infamous egrep/locale bug:
684 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
686 then `texi2dvi' is the superior choice. Org does offer it as one
687 of the customize options.
689 Alternatively, this may be a Lisp function that does the
690 processing, so you could use this to apply the machinery of
691 AUCTeX or the Emacs LaTeX mode. This function should accept the
692 file name as its single argument."
693 :group
'org-export-pdf
695 (repeat :tag
"Shell command sequence"
696 (string :tag
"Shell command"))
697 (const :tag
"2 runs of pdflatex"
698 ("pdflatex -interaction nonstopmode -output-directory %o %f"
699 "pdflatex -interaction nonstopmode -output-directory %o %f"))
700 (const :tag
"3 runs of pdflatex"
701 ("pdflatex -interaction nonstopmode -output-directory %o %f"
702 "pdflatex -interaction nonstopmode -output-directory %o %f"
703 "pdflatex -interaction nonstopmode -output-directory %o %f"))
704 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
705 ("pdflatex -interaction nonstopmode -output-directory %o %f"
707 "pdflatex -interaction nonstopmode -output-directory %o %f"
708 "pdflatex -interaction nonstopmode -output-directory %o %f"))
709 (const :tag
"texi2dvi"
710 ("texi2dvi -p -b -c -V %f"))
712 ("rubber -d --into %o %f"))
715 (defcustom org-e-latex-logfiles-extensions
716 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
717 "The list of file extensions to consider as LaTeX logfiles."
718 :group
'org-export-e-latex
719 :type
'(repeat (string :tag
"Extension")))
721 (defcustom org-e-latex-remove-logfiles t
722 "Non-nil means remove the logfiles produced by PDF production.
723 These are the .aux, .log, .out, and .toc files."
724 :group
'org-export-e-latex
729 ;;; Internal Functions
731 (defun org-e-latex--caption/label-string
(caption label info
)
732 "Return caption and label LaTeX string for floats.
734 CAPTION is a cons cell of secondary strings, the car being the
735 standard caption and the cdr its short form. LABEL is a string
736 representing the label. INFO is a plist holding contextual
739 If there's no caption nor label, return the empty string.
741 For non-floats, see `org-e-latex--wrap-label'."
742 (let ((label-str (if label
(format "\\label{%s}" label
) "")))
744 ((and (not caption
) (not label
)) "")
745 ((not caption
) (format "\\label{%s}\n" label
))
746 ;; Option caption format with short name.
748 (format "\\caption[%s]{%s%s}\n"
749 (org-export-secondary-string (cdr caption
) info
)
751 (org-export-secondary-string (car caption
) info
)))
752 ;; Standard caption format.
753 (t (format "\\caption{%s%s}\n"
755 (org-export-secondary-string (car caption
) info
))))))
757 (defun org-e-latex--guess-inputenc (header)
758 "Set the coding system in inputenc to what the buffer is.
760 HEADER is the LaTeX header string.
762 Return the new header."
763 (let* ((cs (or (ignore-errors
764 (latexenc-coding-system-to-inputenc
765 buffer-file-coding-system
))
768 ;; First translate if that is requested.
769 (setq cs
(or (cdr (assoc cs org-e-latex-inputenc-alist
)) cs
))
770 ;; Then find the \usepackage statement and replace the option.
771 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
772 cs header t nil
1))))
774 (defun org-e-latex--find-verb-separator (s)
775 "Return a character not used in string S.
776 This is used to choose a separator for constructs like \\verb."
777 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
778 (loop for c across ll
779 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
780 return
(char-to-string c
))))
782 (defun org-e-latex--make-option-string (options)
783 "Return a comma separated string of keywords and values.
784 OPTIONS is an alist where the key is the options keyword as
785 a string, and the value a list containing the keyword value, or
787 (mapconcat (lambda (pair)
789 (when (> (length (second pair
)) 0)
790 (concat "=" (second pair
)))))
794 (defun org-e-latex--quotation-marks (text info
)
795 "Export quotation marks depending on language conventions.
796 TEXT is a string containing quotation marks to be replaced. INFO
797 is a plist used as a communication channel."
800 (while (setq start
(string-match (car l
) text start
))
801 (let ((new-quote (concat (match-string 1 text
) (cdr l
))))
802 (setq text
(replace-match new-quote t t text
))))))
803 (cdr (or (assoc (plist-get info
:language
) org-e-latex-quotes
)
804 ;; Falls back on English.
805 (assoc "en" org-e-latex-quotes
))))
808 (defun org-e-latex--wrap-label (element output
)
809 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
810 This function shouldn't be used for floats. See
811 `org-e-latex--caption/label-string'."
812 (let ((label (org-element-property :name element
)))
813 (if (or (not output
) (not label
) (string= output
"") (string= label
""))
815 (concat (format "\\label{%s}\n" label
) output
))))
817 (defun org-e-latex--text-markup (text markup
)
818 "Format text depending on MARKUP text markup.
819 See `org-e-latex-text-markup-alist' for details."
820 (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist
))))
822 ;; No format string: Return raw text.
824 ;; Handle the `verb' special case: Find and appropriate separator
825 ;; and use "\\verb" command.
827 (let ((separator (org-e-latex--find-verb-separator text
)))
828 (concat "\\verb" separator text separator
)))
829 ;; Handle the `protectedtexttt' special case: Protect some
830 ;; special chars and use "\texttt{%s}" format string.
831 ((eq 'protectedtexttt fmt
)
833 (trans '(("\\" .
"\\textbackslash{}")
834 ("~" .
"\\textasciitilde{}")
835 ("^" .
"\\textasciicircum{}")))
838 (while (string-match "[\\{}$%&_#~^]" text
)
839 (setq char
(match-string 0 text
))
840 (if (> (match-beginning 0) 0)
841 (setq rtn
(concat rtn
(substring value
0 (match-beginning 0)))))
842 (setq text
(substring text
(1+ (match-beginning 0))))
843 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
844 rtn
(concat rtn char
)))
845 (setq text
(concat rtn text
)
847 (while (string-match "--" text
)
848 (setq text
(replace-match "-{}-" t t text
)))
850 ;; Else use format string.
851 (t (format fmt text
)))))
857 (defun org-e-latex-template (contents info
)
858 "Return complete document string after LaTeX conversion.
859 CONTENTS is the transcoded contents string. INFO is a plist
860 holding export options."
861 (let ((title (org-export-secondary-string (plist-get info
:title
) info
)))
864 (and (plist-get info
:time-stamp-file
)
865 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
866 ;; 2. Document class and packages.
867 (let ((class (plist-get info
:latex-class
))
868 (class-options (plist-get info
:latex-class-options
)))
869 (org-element-normalize-string
870 (let* ((header (nth 1 (assoc class org-e-latex-classes
)))
871 (document-class-string
872 (and (stringp header
)
874 (replace-regexp-in-string
875 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
876 class-options header t nil
1)
878 (org-e-latex--guess-inputenc
879 (org-splice-latex-header
880 document-class-string
881 org-export-latex-default-packages-alist
; defined in org.el
882 org-export-latex-packages-alist nil
; defined in org.el
883 (plist-get info
:latex-header-extra
))))))
884 ;; 3. Define alert if not yet defined.
885 "\\providecommand{\\alert}[1]{\\textbf{#1}}\n"
886 ;; 4. Possibly limit depth for headline numbering.
887 (let ((sec-num (plist-get info
:section-numbers
)))
888 (when (integerp sec-num
)
889 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
891 (let ((author (and (plist-get info
:with-author
)
892 (let ((auth (plist-get info
:author
)))
893 (and auth
(org-export-secondary-string auth info
)))))
894 (email (and (plist-get info
:with-email
)
895 (org-export-secondary-string
896 (plist-get info
:email
) info
))))
897 (cond ((and author email
(not (string= "" email
)))
898 (format "\\author{%s\\thanks{%s}}\n" author email
))
899 (author (format "\\author{%s}\n" author
))
902 (let ((date (plist-get info
:date
)))
903 (and date
(format "\\date{%s}\n" date
)))
905 (format "\\title{%s}\n" title
)
906 ;; 8. Hyperref options.
907 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
908 (or (plist-get info
:keywords
) "")
909 (or (plist-get info
:description
) "")
910 (if (not (plist-get info
:with-creator
)) ""
911 (plist-get info
:creator
)))
912 ;; 9. Document start.
913 "\\begin{document}\n\n"
914 ;; 10. Title command.
915 (org-element-normalize-string
916 (cond ((string= "" title
) nil
)
917 ((not (stringp org-e-latex-title-command
)) nil
)
918 ((string-match "\\(?:[^%]\\|^\\)%s"
919 org-e-latex-title-command
)
920 (format org-e-latex-title-command title
))
921 (t org-e-latex-title-command
)))
922 ;; 11. Table of contents.
923 (let ((depth (plist-get info
:with-toc
)))
925 (concat (when (wholenump depth
)
926 (format "\\setcounter{tocdepth}{%d}\n" depth
))
927 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
928 ;; 12. Document's body.
931 (let ((creator-info (plist-get info
:with-creator
)))
933 ((not creator-info
) "")
934 ((eq creator-info
'comment
)
935 (format "%% %s\n" (plist-get info
:creator
)))
936 (t (concat (plist-get info
:creator
) "\n"))))
942 ;;; Transcode Functions
946 (defun org-e-latex-bold (bold contents info
)
947 "Transcode BOLD from Org to LaTeX.
948 CONTENTS is the text with bold markup. INFO is a plist holding
949 contextual information."
950 (org-e-latex--text-markup contents
'bold
))
955 (defun org-e-latex-center-block (center-block contents info
)
956 "Transcode a CENTER-BLOCK element from Org to LaTeX.
957 CONTENTS holds the contents of the center block. INFO is a plist
958 holding contextual information."
959 (org-e-latex--wrap-label
961 (format "\\begin{center}\n%s\\end{center}" contents
)))
966 (defun org-e-latex-code (code contents info
)
967 "Transcode a CODE object from Org to LaTeX.
968 CONTENTS is nil. INFO is a plist used as a communication
970 (org-e-latex--text-markup (org-element-property :value code
) 'code
))
975 ;; Comments are ignored.
980 ;; Comment Blocks are ignored.
985 (defun org-e-latex-drawer (drawer contents info
)
986 "Transcode a DRAWER element from Org to LaTeX.
987 CONTENTS holds the contents of the block. INFO is a plist
988 holding contextual information."
989 (let* ((name (org-element-property :drawer-name drawer
))
990 (output (if (functionp org-e-latex-format-drawer-function
)
991 (funcall org-e-latex-format-drawer-function
993 ;; If there's no user defined function: simply
994 ;; display contents of the drawer.
996 (org-e-latex--wrap-label drawer output
)))
1001 (defun org-e-latex-dynamic-block (dynamic-block contents info
)
1002 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1003 CONTENTS holds the contents of the block. INFO is a plist
1004 holding contextual information. See `org-export-data'."
1005 (org-e-latex--wrap-label dynamic-block contents
))
1010 (defun org-e-latex-entity (entity contents info
)
1011 "Transcode an ENTITY object from Org to LaTeX.
1012 CONTENTS are the definition itself. INFO is a plist holding
1013 contextual information."
1014 (let ((ent (org-element-property :latex entity
)))
1015 (if (org-element-property :latex-math-p entity
) (format "$%s$" ent
) ent
)))
1020 (defun org-e-latex-example-block (example-block contents info
)
1021 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1022 CONTENTS is nil. INFO is a plist holding contextual
1024 (org-e-latex--wrap-label
1026 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1027 (org-export-format-code-default example-block info
))))
1032 (defun org-e-latex-export-snippet (export-snippet contents info
)
1033 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1034 CONTENTS is nil. INFO is a plist holding contextual information."
1035 (when (eq (org-export-snippet-backend export-snippet
) 'e-latex
)
1036 (org-element-property :value export-snippet
)))
1041 (defun org-e-latex-export-block (export-block contents info
)
1042 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1043 CONTENTS is nil. INFO is a plist holding contextual information."
1044 (when (string= (org-element-property :type export-block
) "latex")
1045 (org-remove-indentation (org-element-property :value export-block
))))
1050 (defun org-e-latex-fixed-width (fixed-width contents info
)
1051 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1052 CONTENTS is nil. INFO is a plist holding contextual information."
1053 (let* ((value (org-element-normalize-string
1054 (replace-regexp-in-string
1056 (org-element-property :value fixed-width
)))))
1057 (org-e-latex--wrap-label
1058 fixed-width
(format "\\begin{verbatim}\n%s\\end{verbatim}" value
))))
1061 ;;;; Footnote Definition
1063 ;; Footnote Definitions are ignored.
1066 ;;;; Footnote Reference
1068 (defun org-e-latex-footnote-reference (footnote-reference contents info
)
1069 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1070 CONTENTS is nil. INFO is a plist holding contextual information."
1072 ;; Insert separator between two footnotes in a row.
1073 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1074 (when (eq (org-element-type prev
) 'footnote-reference
)
1075 org-e-latex-footnote-separator
))
1077 ;; Use \footnotemark if the footnote has already been defined.
1078 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1079 (format "\\footnotemark[%s]{}"
1080 (org-export-get-footnote-number footnote-reference info
)))
1081 ;; Use also \footnotemark if reference is within another footnote
1082 ;; reference or footnote definition.
1083 ((loop for parent in
(org-export-get-genealogy footnote-reference info
)
1084 thereis
(memq (org-element-type parent
)
1085 '(footnote-reference footnote-definition
)))
1086 (let ((num (org-export-get-footnote-number footnote-reference info
)))
1087 (format "\\footnotemark[%s]{}\\setcounter{footnote}{%s}" num num
)))
1088 ;; Otherwise, define it with \footnote command.
1090 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1091 (unless (eq (org-element-type def
) 'org-data
)
1092 (setq def
(cons 'org-data
(cons nil def
))))
1094 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1095 ;; Retrieve all footnote references within the footnote and
1096 ;; add their definition after it, since LaTeX doesn't support
1099 search-refs
; for byte-compiler
1103 ;; Return a list of all footnote references in DATA.
1105 data
'footnote-reference
1107 (when (org-export-footnote-first-reference-p ref info
)
1109 (when (eq (org-element-property :type ref
) 'standard
)
1112 (org-export-get-footnote-definition ref info
)))))
1113 info
) (reverse all-refs
)))))
1117 "\\footnotetext[%s]{%s}"
1118 (org-export-get-footnote-number ref info
)
1121 (if (eq (org-element-property :type ref
) 'inline
)
1122 'org-export-secondary-string
1124 (org-export-get-footnote-definition ref info
) info
))))
1125 (funcall search-refs def
) ""))))))))
1130 (defun org-e-latex-headline (headline contents info
)
1131 "Transcode an HEADLINE element from Org to LaTeX.
1132 CONTENTS holds the contents of the headline. INFO is a plist
1133 holding contextual information."
1134 (let* ((class (plist-get info
:latex-class
))
1135 (level (org-export-get-relative-level headline info
))
1136 (numberedp (org-export-numbered-headline-p headline info
))
1137 (class-sectionning (assoc class org-e-latex-classes
))
1138 ;; Section formatting will set two placeholders: one for the
1139 ;; title and the other for the contents.
1141 (let ((sec (if (and (symbolp (nth 2 class-sectionning
))
1142 (fboundp (nth 2 class-sectionning
)))
1143 (funcall (nth 2 class-sectionning
) level numberedp
)
1144 (nth (1+ level
) class-sectionning
))))
1146 ;; No section available for that LEVEL.
1148 ;; Section format directly returned by a function.
1150 ;; (numbered-section . unnumbered-section)
1151 ((not (consp (cdr sec
)))
1152 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1153 ;; (numbered-open numbered-close)
1155 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1156 ;; (num-in num-out no-num-in no-num-out)
1158 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1159 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1160 (text (org-export-secondary-string
1161 (org-element-property :title headline
) info
))
1163 (and (plist-get info
:with-todo-keywords
)
1164 (let ((todo (org-element-property :todo-keyword headline
)))
1165 (and todo
(org-export-secondary-string todo info
)))))
1166 (todo-type (and todo
(org-element-property :todo-type headline
)))
1167 (tags (and (plist-get info
:with-tags
)
1168 (org-element-property :tags headline
)))
1169 (priority (and (plist-get info
:with-priority
)
1170 (org-element-property :priority headline
)))
1171 ;; Create the headline text.
1172 (full-text (if (functionp org-e-latex-format-headline-function
)
1173 ;; User-defined formatting function.
1174 (funcall org-e-latex-format-headline-function
1175 todo todo-type priority text tags
)
1176 ;; Default formatting.
1179 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1180 (when priority
(format "\\framebox{\\#%c} " priority
))
1182 (when tags
(format "\\hfill{}\\textsc{%s}" tags
)))))
1183 ;; Associate some \label to the headline for internal links.
1185 (format "\\label{sec-%s}\n"
1186 (mapconcat 'number-to-string
1187 (org-export-get-headline-number headline info
)
1190 (make-string (org-element-property :pre-blank headline
) 10)))
1192 ;; Case 1: This is a footnote section: ignore it.
1193 ((org-element-property :footnote-section-p headline
) nil
)
1194 ;; Case 2. This is a deep sub-tree: export it as a list item.
1195 ;; Also export as items headlines for which no section
1196 ;; format has been found.
1197 ((or (not section-fmt
) (org-export-low-level-p headline info
))
1198 ;; Build the real contents of the sub-tree.
1199 (let ((low-level-body
1201 ;; If the headline is the first sibling, start a list.
1202 (when (org-export-first-sibling-p headline info
)
1203 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1205 "\\item " full-text
"\n" headline-label pre-blanks contents
)))
1206 ;; If headline is not the last sibling simply return
1207 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1209 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1210 (replace-regexp-in-string
1212 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1214 ;; Case 3. Standard headline. Export it as a section.
1215 (t (format section-fmt full-text
1216 (concat headline-label pre-blanks contents
))))))
1219 ;;;; Horizontal Rule
1221 (defun org-e-latex-horizontal-rule (horizontal-rule contents info
)
1222 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1223 CONTENTS is nil. INFO is a plist holding contextual information."
1224 (let ((attr (mapconcat #'identity
1225 (org-element-property :attr_latex horizontal-rule
)
1227 (org-e-latex--wrap-label horizontal-rule
(concat "\\hrule " attr
))))
1230 ;;;; Inline Babel Call
1232 ;; Inline Babel Calls are ignored.
1235 ;;;; Inline Src Block
1237 (defun org-e-latex-inline-src-block (inline-src-block contents info
)
1238 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1239 CONTENTS holds the contents of the item. INFO is a plist holding
1240 contextual information."
1241 (let* ((code (org-element-property :value inline-src-block
))
1242 (separator (org-e-latex--find-verb-separator code
)))
1244 ;; Do not use a special package: transcode it verbatim.
1245 ((not org-e-latex-listings
)
1246 (concat "\\verb" separator code separator
))
1247 ;; Use minted package.
1248 ((eq org-e-latex-listings
'minted
)
1249 (let* ((org-lang (org-element-property :language inline-src-block
))
1250 (mint-lang (or (cadr (assq (intern org-lang
)
1251 org-e-latex-minted-langs
))
1253 (options (org-e-latex--make-option-string
1254 org-e-latex-minted-options
)))
1255 (concat (format "\\mint%s{%s}"
1256 (if (string= options
"") "" (format "[%s]" options
))
1258 separator code separator
)))
1259 ;; Use listings package.
1261 ;; Maybe translate language's name.
1262 (let* ((org-lang (org-element-property :language inline-src-block
))
1263 (lst-lang (or (cadr (assq (intern org-lang
)
1264 org-e-latex-listings-langs
))
1266 (options (org-e-latex--make-option-string
1267 (append org-e-latex-listings-options
1268 `(("language" ,lst-lang
))))))
1269 (concat (format "\\lstinline[%s]" options
)
1270 separator code separator
))))))
1275 (defun org-e-latex-inlinetask (inlinetask contents info
)
1276 "Transcode an INLINETASK element from Org to LaTeX.
1277 CONTENTS holds the contents of the block. INFO is a plist
1278 holding contextual information."
1279 (let ((title (org-export-secondary-string
1280 (org-element-property :title inlinetask
) info
))
1281 (todo (and (plist-get info
:with-todo-keywords
)
1282 (let ((todo (org-element-property
1283 :todo-keyword inlinetask
)))
1285 (org-export-secondary-string todo info
)))))
1286 (todo-type (org-element-property :todo-type inlinetask
))
1287 (tags (and (plist-get info
:with-tags
)
1288 (org-element-property :tags inlinetask
)))
1289 (priority (and (plist-get info
:with-priority
)
1290 (org-element-property :priority inlinetask
))))
1291 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1292 ;; with appropriate arguments.
1293 (if (functionp org-e-latex-format-inlinetask-function
)
1294 (funcall org-e-latex-format-inlinetask-function
1295 todo todo-type priority title tags contents
)
1296 ;; Otherwise, use a default template.
1297 (org-e-latex--wrap-label
1301 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1302 (when priority
(format "\\framebox{\\#%c} " priority
))
1304 (when tags
(format "\\hfill{}\\textsc{%s}" tags
)))))
1305 (format (concat "\\begin{center}\n"
1307 "\\begin{minipage}[c]{.6\\textwidth}\n"
1309 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1314 full-title contents
))))))
1319 (defun org-e-latex-italic (italic contents info
)
1320 "Transcode ITALIC from Org to LaTeX.
1321 CONTENTS is the text with italic markup. INFO is a plist holding
1322 contextual information."
1323 (org-e-latex--text-markup contents
'italic
))
1328 (defun org-e-latex-item (item contents info
)
1329 "Transcode an ITEM element from Org to LaTeX.
1330 CONTENTS holds the contents of the item. INFO is a plist holding
1331 contextual information."
1332 ;; Grab `:level' from plain-list properties, which is always the
1333 ;; first element above current item.
1334 (let* ((level (org-element-property :level
(org-export-get-parent item info
)))
1335 (counter (let ((count (org-element-property :counter item
)))
1338 (format "\\setcounter{enum%s}{%s}\n"
1339 (nth level
'("i" "ii" "iii" "iv"))
1341 (checkbox (let ((checkbox (org-element-property :checkbox item
)))
1342 (cond ((eq checkbox
'on
) "$\\boxtimes$ ")
1343 ((eq checkbox
'off
) "$\\Box$ ")
1344 ((eq checkbox
'trans
) "$\\boxminus$ "))))
1345 (tag (let ((tag (org-element-property :tag item
)))
1347 (format "[%s]" (org-export-secondary-string tag info
))))))
1348 (concat counter
"\\item" tag
" " checkbox contents
)))
1353 (defun org-e-latex-keyword (keyword contents info
)
1354 "Transcode a KEYWORD element from Org to LaTeX.
1355 CONTENTS is nil. INFO is a plist holding contextual information."
1356 (let ((key (org-element-property :key keyword
))
1357 (value (org-element-property :value keyword
)))
1359 ((string= key
"LATEX") value
)
1360 ((string= key
"INDEX") (format "\\index{%s}" value
))
1361 ;; Invisible targets.
1362 ((string= key
"TARGET") nil
)
1363 ((string= key
"TOC")
1364 (let ((value (downcase value
)))
1366 ((string-match "\\<headlines\\>" value
)
1367 (let ((depth (or (and (string-match "[0-9]+" value
)
1368 (string-to-number (match-string 0 value
)))
1369 (plist-get info
:with-toc
))))
1371 (when (wholenump depth
)
1372 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1373 "\\tableofcontents")))
1374 ((string= "tables" value
) "\\listoftables")
1375 ((string= "figures" value
) "\\listoffigures")
1376 ((string= "listings" value
)
1378 ((eq org-e-latex-listings
'minted
) "\\listoflistings")
1379 (org-e-latex-listings "\\lstlistoflistings")
1380 ;; At the moment, src blocks with a caption are wrapped
1381 ;; into a figure environment.
1382 (t "\\listoffigures")))))))))
1385 ;;;; Latex Environment
1387 (defun org-e-latex-latex-environment (latex-environment contents info
)
1388 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1389 CONTENTS is nil. INFO is a plist holding contextual information."
1390 (let ((label (org-element-property :name latex-environment
))
1391 (value (org-remove-indentation
1392 (org-element-property :value latex-environment
))))
1393 (if (not (org-string-nw-p label
)) value
1394 ;; Environment is labelled: label must be within the environment
1395 ;; (otherwise, a reference pointing to that element will count
1396 ;; the section instead).
1399 (goto-char (point-min))
1401 (insert (format "\\label{%s}\n" label
))
1407 (defun org-e-latex-latex-fragment (latex-fragment contents info
)
1408 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1409 CONTENTS is nil. INFO is a plist holding contextual information."
1410 (org-element-property :value latex-fragment
))
1415 (defun org-e-latex-line-break (line-break contents info
)
1416 "Transcode a LINE-BREAK object from Org to LaTeX.
1417 CONTENTS is nil. INFO is a plist holding contextual information."
1423 (defun org-e-latex-link--inline-image (link info
)
1424 "Return LaTeX code for an inline image.
1425 LINK is the link pointing to the inline image. INFO is a plist
1426 used as a communication channel."
1427 (let* ((parent (org-export-get-parent-paragraph link info
))
1428 (path (let ((raw-path (org-element-property :path link
)))
1429 (if (not (file-name-absolute-p raw-path
)) raw-path
1430 (expand-file-name raw-path
))))
1431 (caption (org-e-latex--caption/label-string
1432 (org-element-property :caption parent
)
1433 (org-element-property :name parent
)
1435 ;; Retrieve latex attributes from the element around.
1436 (attr (let ((raw-attr
1437 (mapconcat #'identity
1438 (org-element-property :attr_latex parent
)
1440 (unless (string= raw-attr
"") raw-attr
)))
1443 ((and attr
(string-match "\\<wrap\\>" attr
)) 'wrap
)
1444 ((and attr
(string-match "\\<multicolumn\\>" attr
)) 'multicolumn
)
1445 ((or (and attr
(string-match "\\<float\\>" attr
))
1446 (not (string= caption
"")))
1450 ((and attr
(string-match "\\<placement=\\(\\S-+\\)" attr
))
1451 (org-match-string-no-properties 1 attr
))
1452 ((eq disposition
'wrap
) "{l}{0.5\\textwidth}")
1453 ((eq disposition
'float
)
1454 (concat "[" org-e-latex-default-figure-position
"]"))
1456 ;; Now clear ATTR from any special keyword and set a default
1457 ;; value if nothing is left.
1461 (replace-regexp-in-string
1462 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr
))))
1463 (setq attr
(cond ((not (string= attr
"")) attr
)
1464 ((eq disposition
'float
) "width=0.7\\textwidth")
1465 ((eq disposition
'wrap
) "width=0.48\\textwidth")
1466 (t (or org-e-latex-image-default-option
""))))
1467 ;; Return proper string, depending on DISPOSITION.
1469 (wrap (format "\\begin{wrapfigure}%s
1471 \\includegraphics[%s]{%s}
1472 %s\\end{wrapfigure}" placement attr path caption
))
1473 (mulicolumn (format "\\begin{figure*}%s
1475 \\includegraphics[%s]{%s}
1476 %s\\end{figure*}" placement attr path caption
))
1477 (float (format "\\begin{figure}%s
1479 \\includegraphics[%s]{%s}
1480 %s\\end{figure}" placement attr path caption
))
1481 (t (format "\\includegraphics[%s]{%s}" attr path
)))))
1483 (defun org-e-latex-link (link desc info
)
1484 "Transcode a LINK object from Org to LaTeX.
1486 DESC is the description part of the link, or the empty string.
1487 INFO is a plist holding contextual information. See
1489 (let* ((type (org-element-property :type link
))
1490 (raw-path (org-element-property :path link
))
1491 ;; Ensure DESC really exists, or set it to nil.
1492 (desc (and (not (string= desc
"")) desc
))
1493 (imagep (org-export-inline-image-p
1494 link org-e-latex-inline-image-rules
))
1496 ((member type
'("http" "https" "ftp" "mailto"))
1497 (concat type
":" raw-path
))
1498 ((string= type
"file")
1499 (when (string-match "\\(.+\\)::.+" raw-path
)
1500 (setq raw-path
(match-string 1 raw-path
)))
1501 (if (file-name-absolute-p raw-path
)
1502 (concat "file://" (expand-file-name raw-path
))
1503 ;; TODO: Not implemented yet. Concat also:
1504 ;; (org-export-directory :LaTeX info)
1505 (concat "file://" raw-path
)))
1510 (imagep (org-e-latex-link--inline-image link info
))
1511 ;; Radioed target: Target's name is obtained from original raw
1512 ;; link. Path is parsed and transcoded in order to have a proper
1513 ;; display of the contents.
1514 ((string= type
"radio")
1515 (format "\\hyperref[%s]{%s}"
1516 (org-export-solidify-link-text path
)
1517 (org-export-secondary-string
1518 (org-element-parse-secondary-string
1519 path
(cdr (assq 'radio-target org-element-object-restrictions
)))
1521 ;; Links pointing to an headline: Find destination and build
1522 ;; appropriate referencing command.
1523 ((member type
'("custom-id" "fuzzy" "id"))
1524 (let ((destination (if (string= type
"fuzzy")
1525 (org-export-resolve-fuzzy-link link info
)
1526 (org-export-resolve-id-link link info
))))
1527 (case (org-element-type destination
)
1528 ;; Fuzzy link points nowhere.
1530 (format "\\texttt{%s}"
1532 (org-export-secondary-string
1533 (org-element-property :raw-link link
) info
))))
1534 ;; Fuzzy link points to an invisible target.
1536 ;; LINK points to an headline. If headlines are numbered
1537 ;; and the link has no description, display headline's
1538 ;; number. Otherwise, display description or headline's
1545 (org-export-get-headline-number destination info
)
1547 (if (and (plist-get info
:section-numbers
) (not desc
))
1548 (format "\\ref{%s}" label
)
1549 (format "\\hyperref[%s]{%s}" label
1551 (org-export-secondary-string
1552 (org-element-property :title destination
) info
))))))
1553 ;; Fuzzy link points to a target. Do as above.
1555 (let ((path (org-export-solidify-link-text path
)))
1556 (if (not desc
) (format "\\ref{%s}" path
)
1557 (format "\\hyperref[%s]{%s}" path desc
)))))))
1558 ;; Coderef: replace link with the reference name or the
1559 ;; equivalent line number.
1560 ((string= type
"coderef")
1561 (format (org-export-get-coderef-format path desc
)
1562 (org-export-resolve-coderef path info
)))
1563 ;; Link type is handled by a special function.
1564 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1565 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1566 ;; External link with a description part.
1567 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1568 ;; External link without a description part.
1569 (path (format "\\url{%s}" path
))
1570 ;; No path, only description. Try to do something useful.
1571 (t (format "\\texttt{%s}" desc
)))))
1576 ;; Babel Calls are ignored.
1581 (defun org-e-latex-macro (macro contents info
)
1582 "Transcode a MACRO element from Org to LaTeX.
1583 CONTENTS is nil. INFO is a plist holding contextual information."
1584 ;; Use available tools.
1585 (org-export-expand-macro macro info
))
1590 (defun org-e-latex-paragraph (paragraph contents info
)
1591 "Transcode a PARAGRAPH element from Org to LaTeX.
1592 CONTENTS is the contents of the paragraph, as a string. INFO is
1593 the plist used as a communication channel."
1599 (defun org-e-latex-plain-list (plain-list contents info
)
1600 "Transcode a PLAIN-LIST element from Org to LaTeX.
1601 CONTENTS is the contents of the list. INFO is a plist holding
1602 contextual information."
1603 (let* ((type (org-element-property :type plain-list
))
1604 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1605 "inparadesc" "asparadesc"))
1606 (paralist-regexp (concat
1608 (mapconcat 'identity paralist-types
"\\|")
1610 (attr (mapconcat #'identity
1611 (org-element-property :attr_latex plain-list
)
1616 (format "\\<%s\\>" paralist-regexp
) attr
))
1617 (match-string 1 attr
))
1618 ((eq type
'ordered
) "enumerate")
1619 ((eq type
'unordered
) "itemize")
1620 ((eq type
'descriptive
) "description"))))
1621 (org-e-latex--wrap-label
1623 (format "\\begin{%s}%s\n%s\\end{%s}"
1625 ;; Once special environment, if any, has been removed, the
1626 ;; rest of the attributes will be optional arguments.
1627 ;; They will be put inside square brackets if necessary.
1628 (let ((opt (replace-regexp-in-string
1629 (format " *%s *" paralist-regexp
) "" attr
)))
1630 (cond ((string= opt
"") "")
1631 ((string-match "\\`\\[[^][]+\\]\\'" opt
) opt
)
1632 (t (format "[%s]" opt
))))
1639 (defun org-e-latex-plain-text (text info
)
1640 "Transcode a TEXT string from Org to LaTeX.
1641 TEXT is the string to transcode. INFO is a plist holding
1642 contextual information."
1643 ;; Protect %, #, &, $, ~, ^, _, { and }.
1644 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text
)
1646 (replace-match (format "\\%s" (match-string 2 text
)) nil t text
2)))
1648 (setq text
(replace-regexp-in-string
1649 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1650 "$\\backslash$" text nil t
1))
1651 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1652 (let ((case-fold-search nil
)
1654 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start
)
1655 (setq text
(replace-match
1656 (format "\\%s{}" (match-string 1 text
)) nil t text
)
1657 start
(match-end 0))))
1658 ;; Handle quotation marks
1659 (setq text
(org-e-latex--quotation-marks text info
))
1660 ;; Convert special strings.
1661 (when (plist-get info
:with-special-strings
)
1662 (while (string-match (regexp-quote "...") text
)
1663 (setq text
(replace-match "\\ldots{}" nil t text
))))
1664 ;; Handle break preservation if required.
1665 (when (plist-get info
:preserve-breaks
)
1666 (setq text
(replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1672 ;;;; Property Drawer
1674 (defun org-e-latex-property-drawer (property-drawer contents info
)
1675 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1676 CONTENTS is nil. INFO is a plist holding contextual
1678 ;; The property drawer isn't exported but we want separating blank
1679 ;; lines nonetheless.
1685 (defun org-e-latex-quote-block (quote-block contents info
)
1686 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1687 CONTENTS holds the contents of the block. INFO is a plist
1688 holding contextual information."
1689 (org-e-latex--wrap-label
1691 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
1696 (defun org-e-latex-quote-section (quote-section contents info
)
1697 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1698 CONTENTS is nil. INFO is a plist holding contextual information."
1699 (let ((value (org-remove-indentation
1700 (org-element-property :value quote-section
))))
1701 (when value
(format "\\begin{verbatim}\n%s\\end{verbatim}" value
))))
1706 (defun org-e-latex-section (section contents info
)
1707 "Transcode a SECTION element from Org to LaTeX.
1708 CONTENTS holds the contents of the section. INFO is a plist
1709 holding contextual information."
1715 (defun org-e-latex-radio-target (radio-target text info
)
1716 "Transcode a RADIO-TARGET object from Org to LaTeX.
1717 TEXT is the text of the target. INFO is a plist holding
1718 contextual information."
1719 (format "\\label{%s}%s"
1720 (org-export-solidify-link-text
1721 (org-element-property :value radio-target
))
1727 (defun org-e-latex-special-block (special-block contents info
)
1728 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
1729 CONTENTS holds the contents of the block. INFO is a plist
1730 holding contextual information."
1731 (let ((type (downcase (org-element-property :type special-block
))))
1732 (org-e-latex--wrap-label
1734 (format "\\begin{%s}\n%s\\end{%s}" type contents type
))))
1739 (defun org-e-latex-src-block (src-block contents info
)
1740 "Transcode a SRC-BLOCK element from Org to LaTeX.
1741 CONTENTS holds the contents of the item. INFO is a plist holding
1742 contextual information."
1743 (let* ((lang (org-element-property :language src-block
))
1744 (caption (org-element-property :caption src-block
))
1745 (label (org-element-property :name src-block
))
1746 (custom-env (and lang
1747 (cadr (assq (intern lang
)
1748 org-e-latex-custom-lang-environments
))))
1749 (num-start (case (org-element-property :number-lines src-block
)
1750 (continued (org-export-get-loc src-block info
))
1752 (retain-labels (org-element-property :retain-labels src-block
)))
1754 ;; Case 1. No source fontification.
1755 ((not org-e-latex-listings
)
1756 (let ((caption-str (org-e-latex--caption/label-string caption label info
))
1757 (float-env (when caption
"\\begin{figure}[H]\n%s\n\\end{figure}")))
1761 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1762 (org-export-format-code-default src-block info
))))))
1763 ;; Case 2. Custom environment.
1764 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
1766 (org-export-format-code-default src-block info
)
1768 ;; Case 3. Use minted package.
1769 ((eq org-e-latex-listings
'minted
)
1770 (let ((float-env (when (or label caption
)
1771 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
1772 (org-e-latex--caption/label-string
1773 caption label info
))))
1776 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
1778 (org-e-latex--make-option-string
1779 (if (not num-start
) org-e-latex-minted-options
1780 (append `(("linenos")
1781 ("firstnumber" ,(number-to-string (1+ num-start
))))
1782 org-e-latex-minted-options
)))
1784 (or (cadr (assq (intern lang
) org-e-latex-minted-langs
)) lang
)
1786 (let* ((code-info (org-export-unravel-code src-block
))
1790 (org-split-string (car code-info
) "\n")))))
1791 (org-export-format-code
1793 (lambda (loc num ref
)
1797 ;; Ensure references are flushed to the right,
1798 ;; separated with 6 spaces from the widest line
1800 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
1801 (format "(%s)" ref
)))))
1802 nil
(and retain-labels
(cdr code-info
)))))))
1804 (if float-env
(format float-env body
) body
)))
1805 ;; Case 4. Use listings package.
1808 (or (cadr (assq (intern lang
) org-e-latex-listings-langs
)) lang
))
1811 (let ((main (org-export-secondary-string (car caption
) info
)))
1812 (if (not (cdr caption
)) (format "{%s}" main
)
1815 (org-export-secondary-string (cdr caption
) info
)
1819 (format "\\lstset{%s}\n"
1820 (org-e-latex--make-option-string
1821 (append org-e-latex-listings-options
1822 `(("language" ,lst-lang
))
1823 (when label
`(("label" ,label
)))
1824 (when caption-str
`(("caption" ,caption-str
)))
1825 (cond ((not num-start
) '(("numbers" "none")))
1826 ((zerop num-start
) '(("numbers" "left")))
1827 (t `(("numbers" "left")
1829 ,(number-to-string (1+ num-start
)))))))))
1832 "\\begin{lstlisting}\n%s\\end{lstlisting}"
1833 (let* ((code-info (org-export-unravel-code src-block
))
1837 (org-split-string (car code-info
) "\n")))))
1838 (org-export-format-code
1840 (lambda (loc num ref
)
1844 ;; Ensure references are flushed to the right,
1845 ;; separated with 6 spaces from the widest line of
1847 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
1848 (format "(%s)" ref
)))))
1849 nil
(and retain-labels
(cdr code-info
)))))))))))
1852 ;;;; Statistics Cookie
1854 (defun org-e-latex-statistics-cookie (statistics-cookie contents info
)
1855 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
1856 CONTENTS is nil. INFO is a plist holding contextual information."
1857 (org-element-property :value statistics-cookie
))
1862 (defun org-e-latex-strike-through (strike-through contents info
)
1863 "Transcode STRIKE-THROUGH from Org to LaTeX.
1864 CONTENTS is the text with strike-through markup. INFO is a plist
1865 holding contextual information."
1866 (org-e-latex--text-markup contents
'strike-through
))
1871 (defun org-e-latex-subscript (subscript contents info
)
1872 "Transcode a SUBSCRIPT object from Org to LaTeX.
1873 CONTENTS is the contents of the object. INFO is a plist holding
1874 contextual information."
1875 (format (if (= (length contents
) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents
))
1880 (defun org-e-latex-superscript (superscript contents info
)
1881 "Transcode a SUPERSCRIPT object from Org to LaTeX.
1882 CONTENTS is the contents of the object. INFO is a plist holding
1883 contextual information."
1884 (format (if (= (length contents
) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents
))
1889 (defun org-e-latex-table--format-string (table info
)
1890 "Return an appropriate format string for TABLE.
1892 TABLE-INFO is the plist containing format info about the table,
1893 as returned by `org-export-table-format-info'. INFO is a plist
1894 used as a communication channel.
1896 The format string leaves one placeholder for the body of the
1898 (let* ((label (org-element-property :name table
))
1899 (caption (org-e-latex--caption/label-string
1900 (org-element-property :caption table
) label info
))
1901 (attr (mapconcat 'identity
1902 (org-element-property :attr_latex table
)
1904 ;; Determine alignment string.
1905 (alignment (org-e-latex-table--align-string table info
))
1906 ;; Determine environment for the table: longtable, tabular...
1908 ((not attr
) org-e-latex-default-table-environment
)
1909 ((string-match "\\<longtable\\>" attr
) "longtable")
1910 ((string-match "\\<tabular.?\\>" attr
)
1911 (org-match-string-no-properties 0 attr
))
1912 (t org-e-latex-default-table-environment
)))
1913 ;; If table is a float, determine environment: table, table*
1914 ;; or sidewaystable.
1916 ((string= "longtable" table-env
) nil
)
1917 ((and attr
(string-match "\\<sidewaystable\\>" attr
))
1920 (or (string-match (regexp-quote "table*") attr
)
1921 (string-match "\\<multicolumn\\>" attr
)))
1923 ((or (not (string= caption
"")) label
) "table")))
1924 ;; Extract others display options.
1925 (width (and attr
(string-match "\\<width=\\(\\S-+\\)" attr
)
1926 (org-match-string-no-properties 1 attr
)))
1928 (if (and attr
(string-match "\\<placement=\\(\\S-+\\)" attr
))
1929 (org-match-string-no-properties 1 attr
)
1930 (format "[%s]" org-e-latex-default-figure-position
))))
1931 ;; Prepare the final format string for the table.
1934 ((string= "longtable" table-env
)
1936 "\\begin{longtable}{%s}\n%s%%s%s\\end{longtable}"
1938 (if (or (not org-e-latex-table-caption-above
) (string= "" caption
)) ""
1939 (concat (org-trim caption
) "\\\\\n"))
1940 (if (or org-e-latex-table-caption-above
(string= "" caption
)) ""
1941 (concat (org-trim caption
) "\\\\\n"))))
1943 (t (concat (when float-env
1945 (format "\\begin{%s}%s\n" float-env placement
)
1946 (if org-e-latex-table-caption-above caption
"")))
1947 (when org-e-latex-tables-centered
"\\begin{center}\n")
1948 (format "\\begin{%s}%s{%s}\n%%s\\end{%s}"
1950 (if width
(format "{%s}" width
) "") alignment table-env
)
1951 (when org-e-latex-tables-centered
"\n\\end{center}")
1953 (concat (if org-e-latex-table-caption-above
"" caption
)
1954 (format "\n\\end{%s}" float-env
))))))))
1956 (defun org-e-latex-table--align-string (table info
)
1957 "Return an appropriate LaTeX alignment string.
1958 TABLE is the considered table. INFO is a plist used as
1959 a communication channel."
1960 (let ((attr (mapconcat 'identity
1961 (org-element-property :attr_latex table
)
1963 (if (and attr
(string-match "\\<align=\\(\\S-+\\)" attr
))
1964 (match-string 1 attr
)
1966 ;; Extract column groups and alignment from first (non-rule)
1969 (org-element-map table
'table-row
'identity info
'first-match
)
1972 (let ((borders (org-export-table-cell-borders cell info
)))
1973 ;; Check left border for the first cell only.
1974 (when (and (memq 'left borders
) (not alignment
))
1975 (push "|" alignment
))
1976 (push (case (org-export-table-cell-alignment cell info
)
1981 (when (memq 'right borders
) (push "|" alignment
))))
1983 (apply 'concat
(reverse alignment
))))))
1985 (defun org-e-latex-table (table contents info
)
1986 "Transcode a TABLE element from Org to LaTeX.
1987 CONTENTS is nil. INFO is a plist holding contextual information."
1989 ;; Case 1: verbatim table.
1990 ((or org-e-latex-tables-verbatim
1991 (let ((attr (mapconcat 'identity
1992 (org-element-property :attr_latex table
)
1994 (and attr
(string-match "\\<verbatim\\>" attr
))))
1995 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
1996 ;; Re-create table, without affiliated keywords.
1998 (org-element-interpret-data
1999 `(org-data nil
(table nil
,@(org-element-contents table
)))))))
2000 ;; Case 2: table.el table. Convert it using appropriate tools.
2001 ((eq (org-element-property :type table
) 'table.el
)
2003 ;; Ensure "*org-export-table*" buffer is empty.
2004 (with-current-buffer (get-buffer-create "*org-export-table*")
2006 (let ((output (with-temp-buffer
2007 (insert (org-element-property :value table
))
2009 (re-search-forward "^[ \t]*|[^|]" nil t
)
2010 (table-generate-source 'latex
"*org-export-table*")
2011 (with-current-buffer "*org-export-table*"
2012 (org-trim (buffer-string))))))
2013 (kill-buffer (get-buffer "*org-export-table*"))
2014 ;; Remove left out comments.
2015 (while (string-match "^%.*\n" output
)
2016 (setq output
(replace-match "" t t output
)))
2017 ;; When the "rmlines" attribute is provided, remove all hlines
2018 ;; but the the one separating heading from the table body.
2019 (let ((attr (mapconcat 'identity
2020 (org-element-property :attr_latex table
)
2022 (when (and attr
(string-match "\\<rmlines\\>" attr
))
2023 (let ((n 0) (pos 0))
2024 (while (and (< (length output
) pos
)
2025 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2028 (setq output
(replace-match "" nil nil output
)))))))
2029 (if (not org-e-latex-tables-centered
) output
2030 (format "\\begin{center}\n%s\n\\end{center}" output
))))
2031 ;; Case 3: Standard table.
2032 (t (format (org-e-latex-table--format-string table info
) contents
))))
2037 (defun org-e-latex-table-cell (table-cell contents info
)
2038 "Transcode a TABLE-CELL element from Org to LaTeX.
2039 CONTENTS is the cell contents. INFO is a plist used as
2040 a communication channel."
2041 (concat (if (and contents
2042 org-e-latex-table-scientific-notation
2043 (string-match orgtbl-exp-regexp contents
))
2044 ;; Use appropriate format string for scientific
2046 (format org-e-latex-table-scientific-notation
2047 (match-string 1 contents
)
2048 (match-string 2 contents
))
2050 (when (org-export-get-next-element table-cell info
) " & ")))
2055 (defun org-e-latex-table-row (table-row contents info
)
2056 "Transcode a TABLE-ROW element from Org to LaTeX.
2057 CONTENTS is the contents of the row. INFO is a plist used as
2058 a communication channel."
2059 ;; Rules are ignored since table separators are deduced from
2060 ;; borders of the current row.
2061 (when (eq (org-element-property :type table-row
) 'standard
)
2062 (let* ((attr (mapconcat 'identity
2063 (org-element-property
2064 :attr_latex
(org-export-get-parent table-row info
))
2066 (longtablep (and attr
(string-match "\\<longtable\\>" attr
)))
2068 (or (and attr
(string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr
))
2069 org-e-latex-tables-booktabs
))
2070 ;; TABLE-ROW's borders are extracted from its first cell.
2072 (org-export-table-cell-borders
2073 (car (org-element-contents table-row
)) info
)))
2075 ;; When BOOKTABS are activated enforce top-rule even when no
2076 ;; hline was specifically marked.
2077 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2078 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2081 ;; Special case for long tables. Define header and footers.
2082 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2085 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2088 (if booktabsp
"\\midrule" "\\hline")
2089 (if booktabsp
"\\midrule" "\\hline")
2090 ;; Number of columns.
2091 (cdr (org-export-table-dimensions
2092 (org-export-get-parent-table table-row info
) info
))))
2093 ;; When BOOKTABS are activated enforce bottom rule even when
2094 ;; no hline was specifically marked.
2095 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2096 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2097 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2102 (defun org-e-latex-target (target contents info
)
2103 "Transcode a TARGET object from Org to LaTeX.
2104 CONTENTS is nil. INFO is a plist holding contextual
2106 (format "\\label{%s}"
2107 (org-export-solidify-link-text (org-element-property :value target
))))
2112 (defun org-e-latex-time-stamp (time-stamp contents info
)
2113 "Transcode a TIME-STAMP object from Org to LaTeX.
2114 CONTENTS is nil. INFO is a plist holding contextual
2116 (let ((value (org-element-property :value time-stamp
))
2117 (type (org-element-property :type time-stamp
))
2118 (appt-type (org-element-property :appt-type time-stamp
)))
2119 (concat (cond ((eq appt-type
'scheduled
)
2120 (format "\\textbf{\\textsc{%s}} " org-scheduled-string
))
2121 ((eq appt-type
'deadline
)
2122 (format "\\textbf{\\textsc{%s}} " org-deadline-string
))
2123 ((eq appt-type
'closed
)
2124 (format "\\textbf{\\textsc{%s}} " org-closed-string
)))
2125 (cond ((memq type
'(active active-range
))
2126 (format org-e-latex-active-timestamp-format value
))
2127 ((memq type
'(inactive inactive-range
))
2128 (format org-e-latex-inactive-timestamp-format value
))
2130 (format org-e-latex-diary-timestamp-format value
))))))
2135 (defun org-e-latex-underline (underline contents info
)
2136 "Transcode UNDERLINE from Org to LaTeX.
2137 CONTENTS is the text with underline markup. INFO is a plist
2138 holding contextual information."
2139 (org-e-latex--text-markup contents
'underline
))
2144 (defun org-e-latex-verbatim (verbatim contents info
)
2145 "Transcode a VERBATIM object from Org to LaTeX.
2146 CONTENTS is nil. INFO is a plist used as a communication
2148 (org-e-latex--text-markup (org-element-property :value verbatim
) 'verbatim
))
2153 (defun org-e-latex-verse-block (verse-block contents info
)
2154 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2155 CONTENTS is verse block contents. INFO is a plist holding
2156 contextual information."
2157 (org-e-latex--wrap-label
2159 ;; In a verse environment, add a line break to each newline
2160 ;; character and change each white space at beginning of a line
2161 ;; into a space of 1 em. Also change each blank line with
2162 ;; a vertical space of 1 em.
2164 (setq contents
(replace-regexp-in-string
2165 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2166 (replace-regexp-in-string
2167 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
2168 (while (string-match "^[ \t]+" contents
)
2169 (let ((new-str (format "\\hspace*{%dem}"
2170 (length (match-string 0 contents
)))))
2171 (setq contents
(replace-match new-str nil t contents
))))
2172 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
2176 ;;; Interactive functions
2178 (defun org-e-latex-export-to-latex
2179 (&optional subtreep visible-only body-only ext-plist pub-dir
)
2180 "Export current buffer to a LaTeX file.
2182 If narrowing is active in the current buffer, only export its
2185 If a region is active, export that region.
2187 When optional argument SUBTREEP is non-nil, export the sub-tree
2188 at point, extracting information from the headline properties
2191 When optional argument VISIBLE-ONLY is non-nil, don't export
2192 contents of hidden elements.
2194 When optional argument BODY-ONLY is non-nil, only write code
2195 between \"\\begin{document}\" and \"\\end{document}\".
2197 EXT-PLIST, when provided, is a property list with external
2198 parameters overriding Org default settings, but still inferior to
2199 file-local settings.
2201 When optional argument PUB-DIR is set, use it as the publishing
2204 Return output file's name."
2206 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir
)))
2208 'e-latex outfile subtreep visible-only body-only ext-plist
)))
2210 (defun org-e-latex-export-to-pdf
2211 (&optional subtreep visible-only body-only ext-plist pub-dir
)
2212 "Export current buffer to LaTeX then process through to PDF.
2214 If narrowing is active in the current buffer, only export its
2217 If a region is active, export that region.
2219 When optional argument SUBTREEP is non-nil, export the sub-tree
2220 at point, extracting information from the headline properties
2223 When optional argument VISIBLE-ONLY is non-nil, don't export
2224 contents of hidden elements.
2226 When optional argument BODY-ONLY is non-nil, only write code
2227 between \"\\begin{document}\" and \"\\end{document}\".
2229 EXT-PLIST, when provided, is a property list with external
2230 parameters overriding Org default settings, but still inferior to
2231 file-local settings.
2233 When optional argument PUB-DIR is set, use it as the publishing
2236 Return PDF file's name."
2238 (org-e-latex-compile
2239 (org-e-latex-export-to-latex
2240 subtreep visible-only body-only ext-plist pub-dir
)))
2242 (defun org-e-latex-compile (texfile)
2243 "Compile a TeX file.
2245 TEXFILE is the name of the file being compiled. Processing is
2246 done through the command specified in `org-e-latex-pdf-process'.
2248 Return PDF file name or an error if it couldn't be produced."
2249 (let* ((wconfig (current-window-configuration))
2250 (texfile (file-truename texfile
))
2251 (base (file-name-sans-extension texfile
))
2253 (message (format "Processing LaTeX file %s ..." texfile
))
2257 ;; A function is provided: Apply it.
2258 ((functionp org-e-latex-pdf-process
)
2259 (funcall org-e-latex-pdf-process
(shell-quote-argument texfile
)))
2260 ;; A list is provided: Replace %b, %f and %o with appropriate
2261 ;; values in each command before applying it. Output is
2262 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2263 ((consp org-e-latex-pdf-process
)
2264 (let* ((out-dir (or (file-name-directory texfile
) "./"))
2265 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2269 (replace-regexp-in-string
2270 "%b" (shell-quote-argument base
)
2271 (replace-regexp-in-string
2272 "%f" (shell-quote-argument texfile
)
2273 (replace-regexp-in-string
2274 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
2276 org-e-latex-pdf-process
)
2277 ;; Collect standard errors from output buffer.
2278 (setq errors
(org-e-latex-collect-errors outbuf
))))
2279 (t (error "No valid command to process to PDF")))
2280 (let ((pdffile (concat base
".pdf")))
2281 ;; Check for process failure. Provide collected errors if
2283 (if (not (file-exists-p pdffile
))
2284 (error (concat (format "PDF file %s wasn't produced" pdffile
)
2285 (when errors
(concat ": " errors
))))
2286 ;; Else remove log files, when specified, and signal end of
2287 ;; process to user, along with any error encountered.
2288 (when org-e-latex-remove-logfiles
2289 (dolist (ext org-e-latex-logfiles-extensions
)
2290 (let ((file (concat base
"." ext
)))
2291 (when (file-exists-p file
) (delete-file file
)))))
2292 (message (concat "Process completed"
2293 (if (not errors
) "."
2294 (concat " with errors: " errors
)))))
2295 ;; Return output file name.
2297 (set-window-configuration wconfig
))))
2299 (defun org-e-latex-collect-errors (buffer)
2300 "Collect some kind of errors from \"pdflatex\" command output.
2302 BUFFER is the buffer containing output.
2304 Return collected error types as a string, or nil if there was
2306 (with-current-buffer buffer
2308 (goto-char (point-max))
2309 ;; Find final "pdflatex" run.
2310 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t
)
2311 (let ((case-fold-search t
)
2313 (when (save-excursion
2314 (re-search-forward "Reference.*?undefined" nil t
))
2315 (setq errors
(concat errors
" [undefined reference]")))
2316 (when (save-excursion
2317 (re-search-forward "Citation.*?undefined" nil t
))
2318 (setq errors
(concat errors
" [undefined citation]")))
2319 (when (save-excursion
2320 (re-search-forward "Undefined control sequence" nil t
))
2321 (setq errors
(concat errors
" [undefined control sequence]")))
2322 (when (save-excursion
2323 (re-search-forward "^! LaTeX.*?Error" nil t
))
2324 (setq errors
(concat errors
" [LaTeX error]")))
2325 (when (save-excursion
2326 (re-search-forward "^! Package.*?Error" nil t
))
2327 (setq errors
(concat errors
" [package error]")))
2328 (and (org-string-nw-p errors
) (org-trim errors
)))))))
2331 (provide 'org-e-latex
)
2332 ;;; org-e-latex.el ends here