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
))
41 (defvar org-export-latex-default-packages-alist
)
42 (defvar org-export-latex-packages-alist
)
43 (defvar orgtbl-exp-regexp
)
49 (org-export-define-backend e-latex
50 ((bold . org-e-latex-bold
)
51 (center-block . org-e-latex-center-block
)
52 (clock . org-e-latex-clock
)
53 (code . org-e-latex-code
)
54 (drawer . org-e-latex-drawer
)
55 (dynamic-block . org-e-latex-dynamic-block
)
56 (entity . org-e-latex-entity
)
57 (example-block . org-e-latex-example-block
)
58 (export-block . org-e-latex-export-block
)
59 (export-snippet . org-e-latex-export-snippet
)
60 (fixed-width . org-e-latex-fixed-width
)
61 (footnote-definition . org-e-latex-footnote-definition
)
62 (footnote-reference . org-e-latex-footnote-reference
)
63 (headline . org-e-latex-headline
)
64 (horizontal-rule . org-e-latex-horizontal-rule
)
65 (inline-src-block . org-e-latex-inline-src-block
)
66 (inlinetask . org-e-latex-inlinetask
)
67 (italic . org-e-latex-italic
)
68 (item . org-e-latex-item
)
69 (keyword . org-e-latex-keyword
)
70 (latex-environment . org-e-latex-latex-environment
)
71 (latex-fragment . org-e-latex-latex-fragment
)
72 (line-break . org-e-latex-line-break
)
73 (link . org-e-latex-link
)
74 (macro . org-e-latex-macro
)
75 (paragraph . org-e-latex-paragraph
)
76 (plain-list . org-e-latex-plain-list
)
77 (plain-text . org-e-latex-plain-text
)
78 (planning . org-e-latex-planning
)
79 (property-drawer . org-e-latex-property-drawer
)
80 (quote-block . org-e-latex-quote-block
)
81 (quote-section . org-e-latex-quote-section
)
82 (radio-target . org-e-latex-radio-target
)
83 (section . org-e-latex-section
)
84 (special-block . org-e-latex-special-block
)
85 (src-block . org-e-latex-src-block
)
86 (statistics-cookie . org-e-latex-statistics-cookie
)
87 (strike-through . org-e-latex-strike-through
)
88 (subscript . org-e-latex-subscript
)
89 (superscript . org-e-latex-superscript
)
90 (table . org-e-latex-table
)
91 (table-cell . org-e-latex-table-cell
)
92 (table-row . org-e-latex-table-row
)
93 (target . org-e-latex-target
)
94 (template . org-e-latex-template
)
95 (timestamp . org-e-latex-timestamp
)
96 (underline . org-e-latex-underline
)
97 (verbatim . org-e-latex-verbatim
)
98 (verse-block . org-e-latex-verse-block
))
100 :options-alist
((:date
"DATE" nil org-e-latex-date-format t
)
101 (:latex-class
"LATEX_CLASS" nil org-e-latex-default-class t
)
102 (:latex-class-options
"LATEX_CLASS_OPTIONS" nil nil t
)
103 (:latex-header-extra
"LATEX_HEADER" nil nil newline
)))
107 ;;; Internal Variables
109 (defconst org-e-latex-babel-language-alist
110 '(("af" .
"afrikaans")
112 ("bt-br" .
"brazilian")
118 ("de-at" .
"naustrian")
119 ("de-de" .
"ngerman")
122 ("en-au" .
"australian")
123 ("en-ca" .
"canadian")
124 ("en-gb" .
"british")
126 ("en-nz" .
"newzealand")
127 ("en-us" .
"american")
133 ("fr-ca" .
"canadien")
137 ("id" .
"indonesian")
143 ("no-no" .
"nynorsk")
145 ("pt" .
"portuguese")
149 ("sb" .
"uppersorbian")
157 ("uk" .
"ukrainian"))
158 "Alist between language code and corresponding Babel option.")
162 ;;; User Configurable Variables
164 (defgroup org-export-e-latex nil
165 "Options for exporting Org mode files to LaTeX."
166 :tag
"Org Export LaTeX"
172 (defcustom org-e-latex-default-class
"article"
173 "The default LaTeX class."
174 :group
'org-export-e-latex
175 :type
'(string :tag
"LaTeX class"))
177 (defcustom org-e-latex-classes
179 "\\documentclass[11pt]{article}"
180 ("\\section{%s}" .
"\\section*{%s}")
181 ("\\subsection{%s}" .
"\\subsection*{%s}")
182 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")
183 ("\\paragraph{%s}" .
"\\paragraph*{%s}")
184 ("\\subparagraph{%s}" .
"\\subparagraph*{%s}"))
186 "\\documentclass[11pt]{report}"
187 ("\\part{%s}" .
"\\part*{%s}")
188 ("\\chapter{%s}" .
"\\chapter*{%s}")
189 ("\\section{%s}" .
"\\section*{%s}")
190 ("\\subsection{%s}" .
"\\subsection*{%s}")
191 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}"))
193 "\\documentclass[11pt]{book}"
194 ("\\part{%s}" .
"\\part*{%s}")
195 ("\\chapter{%s}" .
"\\chapter*{%s}")
196 ("\\section{%s}" .
"\\section*{%s}")
197 ("\\subsection{%s}" .
"\\subsection*{%s}")
198 ("\\subsubsection{%s}" .
"\\subsubsection*{%s}")))
199 "Alist of LaTeX classes and associated header and structure.
200 If #+LaTeX_CLASS is set in the buffer, use its value and the
201 associated information. Here is the structure of each cell:
205 \(numbered-section . unnumbered-section\)
211 The HEADER-STRING is the header that will be inserted into the
212 LaTeX file. It should contain the \\documentclass macro, and
213 anything else that is needed for this setup. To this header, the
214 following commands will be added:
216 - Calls to \\usepackage for all packages mentioned in the
217 variables `org-export-latex-default-packages-alist' and
218 `org-export-latex-packages-alist'. Thus, your header
219 definitions should avoid to also request these packages.
221 - Lines specified via \"#+LaTeX_HEADER:\"
223 If you need more control about the sequence in which the header
224 is built up, or if you want to exclude one of these building
225 blocks for a particular class, you can use the following
226 macro-like placeholders.
228 [DEFAULT-PACKAGES] \\usepackage statements for default packages
229 [NO-DEFAULT-PACKAGES] do not include any of the default packages
230 [PACKAGES] \\usepackage statements for packages
231 [NO-PACKAGES] do not include the packages
232 [EXTRA] the stuff from #+LaTeX_HEADER
233 [NO-EXTRA] do not include #+LaTeX_HEADER stuff
237 \\documentclass{article}
238 [NO-DEFAULT-PACKAGES]
240 \\providecommand{\\alert}[1]{\\textbf{#1}}
243 will omit the default packages, and will include the
244 #+LaTeX_HEADER lines, then have a call to \\providecommand, and
245 then place \\usepackage commands based on the content of
246 `org-export-latex-packages-alist'.
248 If your header, `org-export-latex-default-packages-alist' or
249 `org-export-latex-packages-alist' inserts
250 \"\\usepackage[AUTO]{inputenc}\", AUTO will automatically be
251 replaced with a coding system derived from
252 `buffer-file-coding-system'. See also the variable
253 `org-e-latex-inputenc-alist' for a way to influence this
256 The sectioning structure
257 ------------------------
259 The sectioning structure of the class is given by the elements
260 following the header string. For each sectioning level, a number
261 of strings is specified. A %s formatter is mandatory in each
262 section string and will be replaced by the title of the section.
264 Instead of a cons cell \(numbered . unnumbered\), you can also
265 provide a list of 2 or 4 elements,
267 \(numbered-open numbered-close\)
271 \(numbered-open numbered-close unnumbered-open unnumbered-close\)
273 providing opening and closing strings for a LaTeX environment
274 that should represent the document section. The opening clause
275 should have a %s to represent the section title.
277 Instead of a list of sectioning commands, you can also specify
278 a function name. That function will be called with two
279 parameters, the \(reduced) level of the headline, and a predicate
280 non-nil when the headline should be numbered. It must return
281 a format string in which the section title will be added."
282 :group
'org-export-e-latex
284 (list (string :tag
"LaTeX class")
285 (string :tag
"LaTeX header")
286 (repeat :tag
"Levels" :inline t
289 (string :tag
" numbered")
290 (string :tag
"unnumbered"))
291 (list :tag
"Environment"
292 (string :tag
"Opening (numbered)")
293 (string :tag
"Closing (numbered)")
294 (string :tag
"Opening (unnumbered)")
295 (string :tag
"Closing (unnumbered)"))
296 (function :tag
"Hook computing sectioning"))))))
298 (defcustom org-e-latex-inputenc-alist nil
299 "Alist of inputenc coding system names, and what should really be used.
300 For example, adding an entry
302 (\"utf8\" . \"utf8x\")
304 will cause \\usepackage[utf8x]{inputenc} to be used for buffers that
305 are written as utf8 files."
306 :group
'org-export-e-latex
309 (string :tag
"Derived from buffer")
310 (string :tag
"Use this instead"))))
312 (defcustom org-e-latex-date-format
314 "Format string for \\date{...}."
315 :group
'org-export-e-latex
318 (defcustom org-e-latex-title-command
"\\maketitle"
319 "The command used to insert the title just after \\begin{document}.
320 If this string contains the formatting specification \"%s\" then
321 it will be used as a formatting string, passing the title as an
323 :group
'org-export-e-latex
329 (defcustom org-e-latex-format-headline-function nil
330 "Function to format headline text.
332 This function will be called with 5 arguments:
333 TODO the todo keyword (string or nil).
334 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
335 PRIORITY the priority of the headline (integer or nil)
336 TEXT the main headline text (string).
337 TAGS the tags as a list of strings (list of strings or nil).
339 The function result will be used in the section format string.
341 As an example, one could set the variable to the following, in
342 order to reproduce the default set-up:
344 \(defun org-e-latex-format-headline (todo todo-type priority text tags)
345 \"Default format function for an headline.\"
347 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo))
349 \(format \"\\\\framebox{\\\\#%c} \" priority))
352 \(format \"\\\\hfill{}\\\\textsc{%s}\"
353 \(mapconcat 'identity tags \":\"))))"
354 :group
'org-export-e-latex
360 (defcustom org-e-latex-footnote-separator
"\\textsuperscript{,}\\,"
361 "Text used to separate footnotes."
362 :group
'org-export-e-latex
368 (defcustom org-e-latex-active-timestamp-format
"\\textit{%s}"
369 "A printf format string to be applied to active timestamps."
370 :group
'org-export-e-latex
373 (defcustom org-e-latex-inactive-timestamp-format
"\\textit{%s}"
374 "A printf format string to be applied to inactive timestamps."
375 :group
'org-export-e-latex
378 (defcustom org-e-latex-diary-timestamp-format
"\\textit{%s}"
379 "A printf format string to be applied to diary timestamps."
380 :group
'org-export-e-latex
386 (defcustom org-e-latex-image-default-option
"width=.9\\linewidth"
387 "Default option for images."
388 :group
'org-export-e-latex
391 (defcustom org-e-latex-default-figure-position
"htb"
392 "Default position for latex figures."
393 :group
'org-export-e-latex
396 (defcustom org-e-latex-inline-image-rules
397 '(("file" .
"\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
398 "Rules characterizing image files that can be inlined into LaTeX.
400 A rule consists in an association whose key is the type of link
401 to consider, and value is a regexp that will be matched against
404 Note that, by default, the image extension *actually* allowed
405 depend on the way the LaTeX file is processed. When used with
406 pdflatex, pdf, jpg and png images are OK. When processing
407 through dvi to Postscript, only ps and eps are allowed. The
408 default we use here encompasses both."
409 :group
'org-export-e-latex
410 :type
'(alist :key-type
(string :tag
"Type")
411 :value-type
(regexp :tag
"Path")))
413 (defcustom org-e-latex-link-with-unknown-path-format
"\\texttt{%s}"
414 "Format string for links with unknown path type."
415 :group
'org-export-latex
421 (defcustom org-e-latex-default-table-environment
"tabular"
422 "Default environment used to build tables."
423 :group
'org-export-e-latex
426 (defcustom org-e-latex-tables-centered t
427 "When non-nil, tables are exported in a center environment."
428 :group
'org-export-e-latex
431 (defcustom org-e-latex-tables-verbatim nil
432 "When non-nil, tables are exported verbatim."
433 :group
'org-export-e-latex
436 (defcustom org-e-latex-tables-booktabs nil
437 "When non-nil, display tables in a formal \"booktabs\" style.
438 This option assumes that the \"booktabs\" package is properly
439 loaded in the header of the document. This value can be ignored
440 locally with \"booktabs=yes\" and \"booktabs=no\" LaTeX
442 :group
'org-export-e-latex
445 (defcustom org-e-latex-table-caption-above t
446 "When non-nil, place caption string at the beginning of the table.
447 Otherwise, place it near the end."
448 :group
'org-export-e-latex
451 (defcustom org-e-latex-table-scientific-notation
"%s\\,(%s)"
452 "Format string to display numbers in scientific notation.
453 The format should have \"%s\" twice, for mantissa and exponent
454 \(i.e. \"%s\\\\times10^{%s}\").
456 When nil, no transformation is made."
457 :group
'org-export-e-latex
459 (string :tag
"Format string")
460 (const :tag
"No formatting")))
465 (defcustom org-e-latex-text-markup-alist
'((bold .
"\\textbf{%s}")
467 (italic .
"\\emph{%s}")
468 (strike-through .
"\\st{%s}")
469 (underline .
"\\underline{%s}")
470 (verbatim . protectedtexttt
))
471 "Alist of LaTeX expressions to convert text markup.
473 The key must be a symbol among `bold', `code', `italic',
474 `strike-through', `underline' and `verbatim'. The value is
475 a formatting string to wrap fontified text with.
477 Value can also be set to the following symbols: `verb' and
478 `protectedtexttt'. For the former, Org will use \"\\verb\" to
479 create a format string and select a delimiter character that
480 isn't in the string. For the latter, Org will use \"\\texttt\"
481 to typeset and try to protect special characters.
483 If no association can be found for a given markup, text will be
485 :group
'org-export-e-latex
487 :options
'(bold code italic strike-through underline verbatim
))
492 (defcustom org-e-latex-format-drawer-function nil
493 "Function called to format a drawer in LaTeX code.
495 The function must accept two parameters:
496 NAME the drawer name, like \"LOGBOOK\"
497 CONTENTS the contents of the drawer.
499 The function should return the string to be exported.
501 For example, the variable could be set to the following function
502 in order to mimic default behaviour:
504 \(defun org-e-latex-format-drawer-default \(name contents\)
505 \"Format a drawer element for LaTeX export.\"
507 :group
'org-export-e-latex
513 (defcustom org-e-latex-format-inlinetask-function nil
514 "Function called to format an inlinetask in LaTeX code.
516 The function must accept six parameters:
517 TODO the todo keyword, as a string
518 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
519 PRIORITY the inlinetask priority, as a string
520 NAME the inlinetask name, as a string.
521 TAGS the inlinetask tags, as a list of strings.
522 CONTENTS the contents of the inlinetask, as a string.
524 The function should return the string to be exported.
526 For example, the variable could be set to the following function
527 in order to mimic default behaviour:
529 \(defun org-e-latex-format-inlinetask \(todo type priority name tags contents\)
530 \"Format an inline task element for LaTeX export.\"
534 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo))
535 \(when priority (format \"\\\\framebox{\\\\#%c} \" priority))
538 \(format \"\\\\hfill{}\\\\textsc{:%s:}\"
539 \(mapconcat 'identity tags \":\")))))
540 \(format (concat \"\\\\begin{center}\\n\"
542 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
544 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
546 \"\\\\end{minipage}}\"
548 full-title contents))"
549 :group
'org-export-e-latex
555 (defcustom org-e-latex-listings nil
556 "Non-nil means export source code using the listings package.
557 This package will fontify source code, possibly even with color.
558 If you want to use this, you also need to make LaTeX use the
559 listings package, and if you want to have color, the color
560 package. Just add these to `org-export-latex-packages-alist',
561 for example using customize, or with something like:
563 \(require 'org-e-latex)
564 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"listings\"))
565 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"color\"))
569 \(setq org-e-latex-listings 'minted)
571 causes source code to be exported using the minted package as
572 opposed to listings. If you want to use minted, you need to add
573 the minted package to `org-export-latex-packages-alist', for
574 example using customize, or with
576 \(require 'org-e-latex)
577 \(add-to-list 'org-export-latex-packages-alist '\(\"\" \"minted\"))
579 In addition, it is necessary to install pygments
580 \(http://pygments.org), and to configure the variable
581 `org-e-latex-pdf-process' so that the -shell-escape option is
583 :group
'org-export-e-latex
585 (const :tag
"Use listings" t
)
586 (const :tag
"Use minted" 'minted
)
587 (const :tag
"Export verbatim" nil
)))
589 (defcustom org-e-latex-listings-langs
590 '((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
593 (perl "Perl") (cperl "Perl") (python "Python") (ruby "Ruby")
594 (html "HTML") (xml "XML")
595 (tex "TeX") (latex "TeX")
596 (shell-script "bash")
598 (ocaml "Caml") (caml "Caml")
599 (sql "SQL") (sqlite "sql"))
600 "Alist mapping languages to their listing language counterpart.
601 The key is a symbol, the major mode symbol without the \"-mode\".
602 The value is the string that should be inserted as the language
603 parameter for the listings package. If the mode name and the
604 listings name are the same, the language does not need an entry
605 in this list - but it does not hurt if it is present."
606 :group
'org-export-e-latex
609 (symbol :tag
"Major mode ")
610 (string :tag
"Listings language"))))
612 (defcustom org-e-latex-listings-options nil
613 "Association list of options for the latex listings package.
615 These options are supplied as a comma-separated list to the
616 \\lstset command. Each element of the association list should be
617 a list containing two strings: the name of the option, and the
620 (setq org-e-latex-listings-options
621 '((\"basicstyle\" \"\\small\")
622 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
624 will typeset the code in a small size font with underlined, bold
627 Note that the same options will be applied to blocks of all
629 :group
'org-export-e-latex
632 (string :tag
"Listings option name ")
633 (string :tag
"Listings option value"))))
635 (defcustom org-e-latex-minted-langs
636 '((emacs-lisp "common-lisp")
639 (shell-script "bash")
641 "Alist mapping languages to their minted language counterpart.
642 The key is a symbol, the major mode symbol without the \"-mode\".
643 The value is the string that should be inserted as the language
644 parameter for the minted package. If the mode name and the
645 listings name are the same, the language does not need an entry
646 in this list - but it does not hurt if it is present.
648 Note that minted uses all lower case for language identifiers,
649 and that the full list of language identifiers can be obtained
652 pygmentize -L lexers"
653 :group
'org-export-e-latex
656 (symbol :tag
"Major mode ")
657 (string :tag
"Minted language"))))
659 (defcustom org-e-latex-minted-options nil
660 "Association list of options for the latex minted package.
662 These options are supplied within square brackets in
663 \\begin{minted} environments. Each element of the alist should
664 be a list containing two strings: the name of the option, and the
667 \(setq org-e-latex-minted-options
668 '\((\"bgcolor\" \"bg\") \(\"frame\" \"lines\")))
670 will result in src blocks being exported with
672 \\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
674 as the start of the minted environment. Note that the same
675 options will be applied to blocks of all languages."
676 :group
'org-export-e-latex
679 (string :tag
"Minted option name ")
680 (string :tag
"Minted option value"))))
682 (defvar org-e-latex-custom-lang-environments nil
683 "Alist mapping languages to language-specific LaTeX environments.
685 It is used during export of src blocks by the listings and minted
686 latex packages. For example,
688 \(setq org-e-latex-custom-lang-environments
689 '\(\(python \"pythoncode\"\)\)\)
691 would have the effect that if org encounters begin_src python
692 during latex export it will output
701 (defcustom org-e-latex-quotes
703 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"«~")
704 ("\\(\\S-\\)\"" .
"~»")
705 ("\\(\\s-\\|(\\|^\\)'" .
"'"))
707 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"``")
708 ("\\(\\S-\\)\"" .
"''")
709 ("\\(\\s-\\|(\\|^\\)'" .
"`")))
710 "Alist for quotes to use when converting english double-quotes.
712 The CAR of each item in this alist is the language code.
713 The CDR of each item in this alist is a list of three CONS:
714 - the first CONS defines the opening quote;
715 - the second CONS defines the closing quote;
716 - the last CONS defines single quotes.
718 For each item in a CONS, the first string is a regexp
719 for allowed characters before/after the quote, the second
720 string defines the replacement string for this quote."
721 :group
'org-export-e-latex
723 (cons :tag
"Opening quote"
724 (string :tag
"Regexp for char before")
725 (string :tag
"Replacement quote "))
726 (cons :tag
"Closing quote"
727 (string :tag
"Regexp for char after ")
728 (string :tag
"Replacement quote "))
729 (cons :tag
"Single quote"
730 (string :tag
"Regexp for char before")
731 (string :tag
"Replacement quote "))))
736 (defcustom org-e-latex-pdf-process
737 '("pdflatex -interaction nonstopmode -output-directory %o %f"
738 "pdflatex -interaction nonstopmode -output-directory %o %f"
739 "pdflatex -interaction nonstopmode -output-directory %o %f")
740 "Commands to process a LaTeX file to a PDF file.
741 This is a list of strings, each of them will be given to the
742 shell as a command. %f in the command will be replaced by the
743 full file name, %b by the file base name \(i.e. without
744 extension) and %o by the base directory of the file.
746 The reason why this is a list is that it usually takes several
747 runs of `pdflatex', maybe mixed with a call to `bibtex'. Org
748 does not have a clever mechanism to detect which of these
749 commands have to be run to get to a stable result, and it also
750 does not do any error checking.
752 By default, Org uses 3 runs of `pdflatex' to do the processing.
753 If you have texi2dvi on your system and if that does not cause
754 the infamous egrep/locale bug:
756 http://lists.gnu.org/archive/html/bug-texinfo/2010-03/msg00031.html
758 then `texi2dvi' is the superior choice. Org does offer it as one
759 of the customize options.
761 Alternatively, this may be a Lisp function that does the
762 processing, so you could use this to apply the machinery of
763 AUCTeX or the Emacs LaTeX mode. This function should accept the
764 file name as its single argument."
765 :group
'org-export-pdf
767 (repeat :tag
"Shell command sequence"
768 (string :tag
"Shell command"))
769 (const :tag
"2 runs of pdflatex"
770 ("pdflatex -interaction nonstopmode -output-directory %o %f"
771 "pdflatex -interaction nonstopmode -output-directory %o %f"))
772 (const :tag
"3 runs of pdflatex"
773 ("pdflatex -interaction nonstopmode -output-directory %o %f"
774 "pdflatex -interaction nonstopmode -output-directory %o %f"
775 "pdflatex -interaction nonstopmode -output-directory %o %f"))
776 (const :tag
"pdflatex,bibtex,pdflatex,pdflatex"
777 ("pdflatex -interaction nonstopmode -output-directory %o %f"
779 "pdflatex -interaction nonstopmode -output-directory %o %f"
780 "pdflatex -interaction nonstopmode -output-directory %o %f"))
781 (const :tag
"texi2dvi"
782 ("texi2dvi -p -b -c -V %f"))
784 ("rubber -d --into %o %f"))
787 (defcustom org-e-latex-logfiles-extensions
788 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
789 "The list of file extensions to consider as LaTeX logfiles."
790 :group
'org-export-e-latex
791 :type
'(repeat (string :tag
"Extension")))
793 (defcustom org-e-latex-remove-logfiles t
794 "Non-nil means remove the logfiles produced by PDF production.
795 These are the .aux, .log, .out, and .toc files."
796 :group
'org-export-e-latex
801 ;;; Internal Functions
803 (defun org-e-latex--caption/label-string
(caption label info
)
804 "Return caption and label LaTeX string for floats.
806 CAPTION is a cons cell of secondary strings, the car being the
807 standard caption and the cdr its short form. LABEL is a string
808 representing the label. INFO is a plist holding contextual
811 If there's no caption nor label, return the empty string.
813 For non-floats, see `org-e-latex--wrap-label'."
814 (let ((label-str (if label
(format "\\label{%s}" label
) "")))
816 ((and (not caption
) (not label
)) "")
817 ((not caption
) (format "\\label{%s}\n" label
))
818 ;; Option caption format with short name.
820 (format "\\caption[%s]{%s%s}\n"
821 (org-export-data (cdr caption
) info
)
823 (org-export-data (car caption
) info
)))
824 ;; Standard caption format.
825 (t (format "\\caption{%s%s}\n"
827 (org-export-data (car caption
) info
))))))
829 (defun org-e-latex--guess-babel-language (header info
)
830 "Set Babel's language according to LANGUAGE keyword.
832 HEADER is the LaTeX header string. INFO is the plist used as
833 a communication channel.
835 Insertion of guessed language only happens when Babel package has
836 explicitly been loaded. Then it is added to the rest of
839 Return the new header."
840 (let ((language-code (plist-get info
:language
)))
841 ;; If no language is set or Babel package is not loaded, return
843 (if (or (not (stringp language-code
))
844 (not (string-match "\\\\usepackage\\[\\(.*\\)\\]{babel}" header
)))
846 (let ((options (save-match-data
847 (org-split-string (match-string 1 header
) ",")))
848 (language (cdr (assoc language-code
849 org-e-latex-babel-language-alist
))))
850 ;; If LANGUAGE is already loaded, return header. Otherwise,
851 ;; append LANGUAGE to other options.
852 (if (member language options
) header
853 (replace-match (mapconcat 'identity
854 (append options
(list language
))
856 nil nil header
1))))))
858 (defun org-e-latex--guess-inputenc (header)
859 "Set the coding system in inputenc to what the buffer is.
860 HEADER is the LaTeX header string. Return the new header."
861 (let* ((cs (or (ignore-errors
862 (latexenc-coding-system-to-inputenc
863 buffer-file-coding-system
))
866 ;; First translate if that is requested.
867 (setq cs
(or (cdr (assoc cs org-e-latex-inputenc-alist
)) cs
))
868 ;; Then find the \usepackage statement and replace the option.
869 (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
870 cs header t nil
1))))
872 (defun org-e-latex--find-verb-separator (s)
873 "Return a character not used in string S.
874 This is used to choose a separator for constructs like \\verb."
875 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
876 (loop for c across ll
877 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
878 return
(char-to-string c
))))
880 (defun org-e-latex--make-option-string (options)
881 "Return a comma separated string of keywords and values.
882 OPTIONS is an alist where the key is the options keyword as
883 a string, and the value a list containing the keyword value, or
885 (mapconcat (lambda (pair)
887 (when (> (length (second pair
)) 0)
888 (concat "=" (second pair
)))))
892 (defun org-e-latex--quotation-marks (text info
)
893 "Export quotation marks depending on language conventions.
894 TEXT is a string containing quotation marks to be replaced. INFO
895 is a plist used as a communication channel."
898 (while (setq start
(string-match (car l
) text start
))
899 (let ((new-quote (concat (match-string 1 text
) (cdr l
))))
900 (setq text
(replace-match new-quote t t text
))))))
901 (cdr (or (assoc (plist-get info
:language
) org-e-latex-quotes
)
902 ;; Falls back on English.
903 (assoc "en" org-e-latex-quotes
))))
906 (defun org-e-latex--wrap-label (element output
)
907 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
908 This function shouldn't be used for floats. See
909 `org-e-latex--caption/label-string'."
910 (let ((label (org-element-property :name element
)))
911 (if (or (not output
) (not label
) (string= output
"") (string= label
""))
913 (concat (format "\\label{%s}\n" label
) output
))))
915 (defun org-e-latex--text-markup (text markup
)
916 "Format TEXT depending on MARKUP text markup.
917 See `org-e-latex-text-markup-alist' for details."
918 (let ((fmt (cdr (assq markup org-e-latex-text-markup-alist
))))
920 ;; No format string: Return raw text.
922 ;; Handle the `verb' special case: Find and appropriate separator
923 ;; and use "\\verb" command.
925 (let ((separator (org-e-latex--find-verb-separator text
)))
926 (concat "\\verb" separator text separator
)))
927 ;; Handle the `protectedtexttt' special case: Protect some
928 ;; special chars and use "\texttt{%s}" format string.
929 ((eq 'protectedtexttt fmt
)
931 (trans '(("\\" .
"\\textbackslash{}")
932 ("~" .
"\\textasciitilde{}")
933 ("^" .
"\\textasciicircum{}")))
936 (while (string-match "[\\{}$%&_#~^]" text
)
937 (setq char
(match-string 0 text
))
938 (if (> (match-beginning 0) 0)
939 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
940 (setq text
(substring text
(1+ (match-beginning 0))))
941 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
942 rtn
(concat rtn char
)))
943 (setq text
(concat rtn text
)
945 (while (string-match "--" text
)
946 (setq text
(replace-match "-{}-" t t text
)))
948 ;; Else use format string.
949 (t (format fmt text
)))))
951 (defun org-e-latex--delayed-footnotes-definitions (element info
)
952 "Return footnotes definitions in ELEMENT as a string.
954 INFO is a plist used as a communication channel.
956 Footnotes definitions are returned within \"\\footnotetxt{}\"
959 This functions is used within constructs that don't support
960 \"\\footnote{}\" command (i.e. an item's tag). In that case,
961 \"\\footnotemark\" is used within the construct and this function
966 "\\footnotetext[%s]{%s}"
967 (org-export-get-footnote-number ref info
)
970 (org-export-get-footnote-definition ref info
) info
))))
971 ;; Find every footnote reference in ELEMENT.
973 search-refs
; For byte-compiler.
977 ;; Return a list of all footnote references never seen
980 data
'footnote-reference
982 (when (org-export-footnote-first-reference-p ref info
)
984 (when (eq (org-element-property :type ref
) 'standard
)
986 (org-export-get-footnote-definition ref info
)))))
988 (reverse all-refs
)))))
989 (funcall search-refs element
))
996 (defun org-e-latex-template (contents info
)
997 "Return complete document string after LaTeX conversion.
998 CONTENTS is the transcoded contents string. INFO is a plist
999 holding export options."
1000 (let ((title (org-export-data (plist-get info
:title
) info
)))
1003 (and (plist-get info
:time-stamp-file
)
1004 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
1005 ;; Document class and packages.
1006 (let ((class (plist-get info
:latex-class
))
1007 (class-options (plist-get info
:latex-class-options
)))
1008 (org-element-normalize-string
1009 (let* ((header (nth 1 (assoc class org-e-latex-classes
)))
1010 (document-class-string
1011 (and (stringp header
)
1013 (replace-regexp-in-string
1014 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
1015 class-options header t nil
1)
1017 (when document-class-string
1018 (org-e-latex--guess-babel-language
1019 (org-e-latex--guess-inputenc
1020 (org-splice-latex-header
1021 document-class-string
1022 org-export-latex-default-packages-alist
; defined in org.el
1023 org-export-latex-packages-alist nil
; defined in org.el
1024 (plist-get info
:latex-header-extra
)))
1026 ;; Possibly limit depth for headline numbering.
1027 (let ((sec-num (plist-get info
:section-numbers
)))
1028 (when (integerp sec-num
)
1029 (format "\\setcounter{secnumdepth}{%d}\n" sec-num
)))
1031 (let ((author (and (plist-get info
:with-author
)
1032 (let ((auth (plist-get info
:author
)))
1033 (and auth
(org-export-data auth info
)))))
1034 (email (and (plist-get info
:with-email
)
1035 (org-export-data (plist-get info
:email
) info
))))
1036 (cond ((and author email
(not (string= "" email
)))
1037 (format "\\author{%s\\thanks{%s}}\n" author email
))
1038 (author (format "\\author{%s}\n" author
))
1039 (t "\\author{}\n")))
1041 (let ((date (org-export-data (plist-get info
:date
) info
)))
1042 (and date
(format "\\date{%s}\n" date
)))
1044 (format "\\title{%s}\n" title
)
1045 ;; Hyperref options.
1046 (format "\\hypersetup{\n pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}}\n"
1047 (or (plist-get info
:keywords
) "")
1048 (or (plist-get info
:description
) "")
1049 (if (not (plist-get info
:with-creator
)) ""
1050 (plist-get info
:creator
)))
1052 "\\begin{document}\n\n"
1054 (org-element-normalize-string
1055 (cond ((string= "" title
) nil
)
1056 ((not (stringp org-e-latex-title-command
)) nil
)
1057 ((string-match "\\(?:[^%]\\|^\\)%s"
1058 org-e-latex-title-command
)
1059 (format org-e-latex-title-command title
))
1060 (t org-e-latex-title-command
)))
1061 ;; Table of contents.
1062 (let ((depth (plist-get info
:with-toc
)))
1064 (concat (when (wholenump depth
)
1065 (format "\\setcounter{tocdepth}{%d}\n" depth
))
1066 "\\tableofcontents\n\\vspace*{1cm}\n\n")))
1070 (let ((creator-info (plist-get info
:with-creator
)))
1072 ((not creator-info
) "")
1073 ((eq creator-info
'comment
)
1074 (format "%% %s\n" (plist-get info
:creator
)))
1075 (t (concat (plist-get info
:creator
) "\n"))))
1077 "\\end{document}")))
1081 ;;; Transcode Functions
1085 ;; Babel Calls are ignored.
1090 (defun org-e-latex-bold (bold contents info
)
1091 "Transcode BOLD from Org to LaTeX.
1092 CONTENTS is the text with bold markup. INFO is a plist holding
1093 contextual information."
1094 (org-e-latex--text-markup contents
'bold
))
1099 (defun org-e-latex-center-block (center-block contents info
)
1100 "Transcode a CENTER-BLOCK element from Org to LaTeX.
1101 CONTENTS holds the contents of the center block. INFO is a plist
1102 holding contextual information."
1103 (org-e-latex--wrap-label
1105 (format "\\begin{center}\n%s\\end{center}" contents
)))
1110 (defun org-e-latex-clock (clock contents info
)
1111 "Transcode a CLOCK element from Org to LaTeX.
1112 CONTENTS is nil. INFO is a plist holding contextual
1116 (format "\\textbf{%s} " org-clock-string
)
1117 (format org-e-latex-inactive-timestamp-format
1118 (concat (org-translate-time (org-element-property :value clock
))
1119 (let ((time (org-element-property :time clock
)))
1120 (and time
(format " (%s)" time
)))))
1126 (defun org-e-latex-code (code contents info
)
1127 "Transcode a CODE object from Org to LaTeX.
1128 CONTENTS is nil. INFO is a plist used as a communication
1130 (org-e-latex--text-markup (org-element-property :value code
) 'code
))
1135 ;; Comments are ignored.
1140 ;; Comment Blocks are ignored.
1145 (defun org-e-latex-drawer (drawer contents info
)
1146 "Transcode a DRAWER element from Org to LaTeX.
1147 CONTENTS holds the contents of the block. INFO is a plist
1148 holding contextual information."
1149 (let* ((name (org-element-property :drawer-name drawer
))
1150 (output (if (functionp org-e-latex-format-drawer-function
)
1151 (funcall org-e-latex-format-drawer-function
1153 ;; If there's no user defined function: simply
1154 ;; display contents of the drawer.
1156 (org-e-latex--wrap-label drawer output
)))
1161 (defun org-e-latex-dynamic-block (dynamic-block contents info
)
1162 "Transcode a DYNAMIC-BLOCK element from Org to LaTeX.
1163 CONTENTS holds the contents of the block. INFO is a plist
1164 holding contextual information. See `org-export-data'."
1165 (org-e-latex--wrap-label dynamic-block contents
))
1170 (defun org-e-latex-entity (entity contents info
)
1171 "Transcode an ENTITY object from Org to LaTeX.
1172 CONTENTS are the definition itself. INFO is a plist holding
1173 contextual information."
1174 (let ((ent (org-element-property :latex entity
)))
1175 (if (org-element-property :latex-math-p entity
) (format "$%s$" ent
) ent
)))
1180 (defun org-e-latex-example-block (example-block contents info
)
1181 "Transcode an EXAMPLE-BLOCK element from Org to LaTeX.
1182 CONTENTS is nil. INFO is a plist holding contextual
1184 (org-e-latex--wrap-label
1186 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1187 (org-export-format-code-default example-block info
))))
1192 (defun org-e-latex-export-block (export-block contents info
)
1193 "Transcode a EXPORT-BLOCK element from Org to LaTeX.
1194 CONTENTS is nil. INFO is a plist holding contextual information."
1195 (when (string= (org-element-property :type export-block
) "LATEX")
1196 (org-remove-indentation (org-element-property :value export-block
))))
1201 (defun org-e-latex-export-snippet (export-snippet contents info
)
1202 "Transcode a EXPORT-SNIPPET object from Org to LaTeX.
1203 CONTENTS is nil. INFO is a plist holding contextual information."
1204 (when (eq (org-export-snippet-backend export-snippet
) 'e-latex
)
1205 (org-element-property :value export-snippet
)))
1210 (defun org-e-latex-fixed-width (fixed-width contents info
)
1211 "Transcode a FIXED-WIDTH element from Org to LaTeX.
1212 CONTENTS is nil. INFO is a plist holding contextual information."
1213 (org-e-latex--wrap-label
1215 (format "\\begin{verbatim}\n%s\\end{verbatim}"
1216 (org-remove-indentation
1217 (org-element-property :value fixed-width
)))))
1220 ;;;; Footnote Definition
1222 ;; Footnote Definitions are ignored.
1225 ;;;; Footnote Reference
1227 ;; Footnote reference export is handled by
1228 ;; `org-e-latex-footnote-reference'.
1230 ;; Internally, `org-e-latex--get-footnote-counter' is used to restore
1231 ;; the value of the LaTeX "footnote" counter after a jump due to
1232 ;; a reference to an already defined footnote. It is only needed in
1233 ;; item tags since the optional argument to \footnotemark is not
1236 (defun org-e-latex--get-footnote-counter (footnote-reference info
)
1237 "Return \"footnote\" counter before FOOTNOTE-REFERENCE is encountered.
1238 INFO is a plist used as a communication channel."
1239 ;; Find original counter value by counting number of footnote
1240 ;; references appearing for the first time before the current
1241 ;; footnote reference.
1242 (let* ((label (org-element-property :label footnote-reference
))
1244 search-ref
; For byte-compiler.
1248 ;; Search footnote references through DATA, filling
1249 ;; SEEN-REFS along the way.
1251 data
'footnote-reference
1253 (let ((fn-lbl (org-element-property :label fn
)))
1255 ;; Anonymous footnote match: return number.
1256 ((eq fn footnote-reference
) (length seen-refs
))
1257 ;; Anonymous footnote: it's always a new one.
1258 ;; Also, be sure to return nil from the `cond' so
1259 ;; `first-match' doesn't get us out of the loop.
1260 ((not fn-lbl
) (push 'inline seen-refs
) nil
)
1261 ;; Label not seen so far: add it so SEEN-REFS.
1263 ;; Also search for subsequent references in
1264 ;; footnote definition so numbering follows reading
1265 ;; logic. Note that we don't have to care about
1266 ;; inline definitions, since `org-element-map'
1267 ;; already traverse them at the right time.
1268 ((not (member fn-lbl seen-refs
))
1269 (push fn-lbl seen-refs
)
1271 (org-export-get-footnote-definition fn info
))))))
1272 ;; Don't enter footnote definitions since it will happen
1273 ;; when their first reference is found.
1274 info
'first-match
'footnote-definition
)))))
1275 (funcall search-ref
(plist-get info
:parse-tree
))))
1277 (defun org-e-latex-footnote-reference (footnote-reference contents info
)
1278 "Transcode a FOOTNOTE-REFERENCE element from Org to LaTeX.
1279 CONTENTS is nil. INFO is a plist holding contextual information."
1281 ;; Insert separator between two footnotes in a row.
1282 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1283 (when (eq (org-element-type prev
) 'footnote-reference
)
1284 org-e-latex-footnote-separator
))
1286 ;; Use \footnotemark if reference is within an item's tag.
1287 ((eq (org-element-type (org-export-get-parent-element footnote-reference
))
1289 (if (org-export-footnote-first-reference-p footnote-reference info
)
1291 ;; Since we can't specify footnote number as an optional
1292 ;; argument within an item tag, some extra work has to be done
1293 ;; when the footnote has already been referenced. In that
1294 ;; case, set footnote counter to the desired number, use the
1295 ;; footnotemark, then set counter back to its original value.
1297 "\\setcounter{footnote}{%s}\\footnotemark\\setcounter{footnote}{%s}"
1298 (1- (org-export-get-footnote-number footnote-reference info
))
1299 (org-e-latex--get-footnote-counter footnote-reference info
))))
1300 ;; Use \footnotemark if the footnote has already been defined.
1301 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1302 (format "\\footnotemark[%s]{}"
1303 (org-export-get-footnote-number footnote-reference info
)))
1304 ;; Use \footnotemark if reference is within another footnote
1305 ;; reference or footnote definition.
1306 ((loop for parent in
(org-export-get-genealogy footnote-reference
)
1307 thereis
(memq (org-element-type parent
)
1308 '(footnote-reference footnote-definition
)))
1310 ;; Otherwise, define it with \footnote command.
1312 (let ((def (org-export-get-footnote-definition footnote-reference info
)))
1313 (unless (eq (org-element-type def
) 'org-data
)
1314 (setq def
(cons 'org-data
(cons nil def
))))
1316 (format "\\footnote{%s}" (org-trim (org-export-data def info
)))
1317 ;; Retrieve all footnote references within the footnote and
1318 ;; add their definition after it, since LaTeX doesn't support
1320 (org-e-latex--delayed-footnotes-definitions def info
)))))))
1325 (defun org-e-latex-headline (headline contents info
)
1326 "Transcode an HEADLINE element from Org to LaTeX.
1327 CONTENTS holds the contents of the headline. INFO is a plist
1328 holding contextual information."
1329 (let* ((class (plist-get info
:latex-class
))
1330 (level (org-export-get-relative-level headline info
))
1331 (numberedp (org-export-numbered-headline-p headline info
))
1332 (class-sectionning (assoc class org-e-latex-classes
))
1333 ;; Section formatting will set two placeholders: one for the
1334 ;; title and the other for the contents.
1336 (let ((sec (if (and (symbolp (nth 2 class-sectionning
))
1337 (fboundp (nth 2 class-sectionning
)))
1338 (funcall (nth 2 class-sectionning
) level numberedp
)
1339 (nth (1+ level
) class-sectionning
))))
1341 ;; No section available for that LEVEL.
1343 ;; Section format directly returned by a function.
1345 ;; (numbered-section . unnumbered-section)
1346 ((not (consp (cdr sec
)))
1347 (concat (funcall (if numberedp
#'car
#'cdr
) sec
) "\n%s"))
1348 ;; (numbered-open numbered-close)
1350 (when numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))))
1351 ;; (num-in num-out no-num-in no-num-out)
1353 (if numberedp
(concat (car sec
) "\n%s" (nth 1 sec
))
1354 (concat (nth 2 sec
) "\n%s" (nth 3 sec
)))))))
1355 (text (org-export-data (org-element-property :title headline
) info
))
1357 (and (plist-get info
:with-todo-keywords
)
1358 (let ((todo (org-element-property :todo-keyword headline
)))
1359 (and todo
(org-export-data todo info
)))))
1360 (todo-type (and todo
(org-element-property :todo-type headline
)))
1361 (tags (and (plist-get info
:with-tags
)
1362 (org-export-get-tags headline info
)))
1363 (priority (and (plist-get info
:with-priority
)
1364 (org-element-property :priority headline
)))
1365 ;; Create the headline text along with a no-tag version. The
1366 ;; latter is required to remove tags from table of contents.
1367 (full-text (if (functionp org-e-latex-format-headline-function
)
1368 ;; User-defined formatting function.
1369 (funcall org-e-latex-format-headline-function
1370 todo todo-type priority text tags
)
1371 ;; Default formatting.
1374 (format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1375 (when priority
(format "\\framebox{\\#%c} " priority
))
1378 (format "\\hfill{}\\textsc{:%s:}"
1379 (mapconcat 'identity tags
":"))))))
1381 (if (functionp org-e-latex-format-headline-function
)
1382 ;; User-defined formatting function.
1383 (funcall org-e-latex-format-headline-function
1384 todo todo-type priority text nil
)
1385 ;; Default formatting.
1387 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1388 (when priority
(format "\\framebox{\\#%c} " priority
))
1390 ;; Associate some \label to the headline for internal links.
1392 (format "\\label{sec-%s}\n"
1393 (mapconcat 'number-to-string
1394 (org-export-get-headline-number headline info
)
1397 (make-string (org-element-property :pre-blank headline
) 10)))
1399 ;; Case 1: This is a footnote section: ignore it.
1400 ((org-element-property :footnote-section-p headline
) nil
)
1401 ;; Case 2. This is a deep sub-tree: export it as a list item.
1402 ;; Also export as items headlines for which no section
1403 ;; format has been found.
1404 ((or (not section-fmt
) (org-export-low-level-p headline info
))
1405 ;; Build the real contents of the sub-tree.
1406 (let ((low-level-body
1408 ;; If the headline is the first sibling, start a list.
1409 (when (org-export-first-sibling-p headline info
)
1410 (format "\\begin{%s}\n" (if numberedp
'enumerate
'itemize
)))
1412 "\\item " full-text
"\n" headline-label pre-blanks contents
)))
1413 ;; If headline is not the last sibling simply return
1414 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1416 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1417 (replace-regexp-in-string
1419 (format "\n\\\\end{%s}" (if numberedp
'enumerate
'itemize
))
1421 ;; Case 3. Standard headline. Export it as a section.
1424 ((not (and tags
(eq (plist-get info
:with-tags
) 'not-in-toc
)))
1425 ;; Regular section. Use specified format string.
1426 (format section-fmt full-text
1427 (concat headline-label pre-blanks contents
)))
1428 ((string-match "\\`\\\\\\(.*?\\){" section-fmt
)
1429 ;; If tags should be removed from table of contents, insert
1430 ;; title without tags as an alternative heading in sectioning
1432 (format (replace-match (concat (match-string 1 section-fmt
) "[%s]")
1433 nil nil section-fmt
1)
1434 ;; Replace square brackets with parenthesis since
1435 ;; square brackets are not supported in optional
1437 (replace-regexp-in-string
1439 (replace-regexp-in-string
1443 (concat headline-label pre-blanks contents
)))
1445 ;; Impossible to add an alternative heading. Fallback to
1446 ;; regular sectioning format string.
1447 (format section-fmt full-text
1448 (concat headline-label pre-blanks contents
))))))))
1451 ;;;; Horizontal Rule
1453 (defun org-e-latex-horizontal-rule (horizontal-rule contents info
)
1454 "Transcode an HORIZONTAL-RULE object from Org to LaTeX.
1455 CONTENTS is nil. INFO is a plist holding contextual information."
1456 (let ((attr (mapconcat #'identity
1457 (org-element-property :attr_latex horizontal-rule
)
1459 (org-e-latex--wrap-label horizontal-rule
(concat "\\hrule " attr
))))
1462 ;;;; Inline Babel Call
1464 ;; Inline Babel Calls are ignored.
1467 ;;;; Inline Src Block
1469 (defun org-e-latex-inline-src-block (inline-src-block contents info
)
1470 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1471 CONTENTS holds the contents of the item. INFO is a plist holding
1472 contextual information."
1473 (let* ((code (org-element-property :value inline-src-block
))
1474 (separator (org-e-latex--find-verb-separator code
)))
1476 ;; Do not use a special package: transcode it verbatim.
1477 ((not org-e-latex-listings
)
1478 (concat "\\verb" separator code separator
))
1479 ;; Use minted package.
1480 ((eq org-e-latex-listings
'minted
)
1481 (let* ((org-lang (org-element-property :language inline-src-block
))
1482 (mint-lang (or (cadr (assq (intern org-lang
)
1483 org-e-latex-minted-langs
))
1485 (options (org-e-latex--make-option-string
1486 org-e-latex-minted-options
)))
1487 (concat (format "\\mint%s{%s}"
1488 (if (string= options
"") "" (format "[%s]" options
))
1490 separator code separator
)))
1491 ;; Use listings package.
1493 ;; Maybe translate language's name.
1494 (let* ((org-lang (org-element-property :language inline-src-block
))
1495 (lst-lang (or (cadr (assq (intern org-lang
)
1496 org-e-latex-listings-langs
))
1498 (options (org-e-latex--make-option-string
1499 (append org-e-latex-listings-options
1500 `(("language" ,lst-lang
))))))
1501 (concat (format "\\lstinline[%s]" options
)
1502 separator code separator
))))))
1507 (defun org-e-latex-inlinetask (inlinetask contents info
)
1508 "Transcode an INLINETASK element from Org to LaTeX.
1509 CONTENTS holds the contents of the block. INFO is a plist
1510 holding contextual information."
1511 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1512 (todo (and (plist-get info
:with-todo-keywords
)
1513 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1514 (and todo
(org-export-data todo info
)))))
1515 (todo-type (org-element-property :todo-type inlinetask
))
1516 (tags (and (plist-get info
:with-tags
)
1517 (org-export-get-tags inlinetask info
)))
1518 (priority (and (plist-get info
:with-priority
)
1519 (org-element-property :priority inlinetask
))))
1520 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1521 ;; with appropriate arguments.
1522 (if (functionp org-e-latex-format-inlinetask-function
)
1523 (funcall org-e-latex-format-inlinetask-function
1524 todo todo-type priority title tags contents
)
1525 ;; Otherwise, use a default template.
1526 (org-e-latex--wrap-label
1530 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1531 (when priority
(format "\\framebox{\\#%c} " priority
))
1533 (when tags
(format "\\hfill{}\\textsc{:%s:}"
1534 (mapconcat 'identity tags
":"))))))
1535 (format (concat "\\begin{center}\n"
1537 "\\begin{minipage}[c]{.6\\textwidth}\n"
1539 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1544 full-title contents
))))))
1549 (defun org-e-latex-italic (italic contents info
)
1550 "Transcode ITALIC from Org to LaTeX.
1551 CONTENTS is the text with italic markup. INFO is a plist holding
1552 contextual information."
1553 (org-e-latex--text-markup contents
'italic
))
1558 (defun org-e-latex-item (item contents info
)
1559 "Transcode an ITEM element from Org to LaTeX.
1560 CONTENTS holds the contents of the item. INFO is a plist holding
1561 contextual information."
1563 (let ((count (org-element-property :counter item
))
1565 (loop for parent in
(org-export-get-genealogy item
)
1566 count
(eq (org-element-type parent
) 'plain-list
)
1567 until
(eq (org-element-type parent
) 'headline
))))
1570 (format "\\setcounter{enum%s}{%s}\n"
1571 (nth (1- level
) '("i" "ii" "iii" "iv"))
1573 (checkbox (case (org-element-property :checkbox item
)
1574 (on "$\\boxtimes$ ")
1576 (trans "$\\boxminus$ ")))
1577 (tag (let ((tag (org-element-property :tag item
)))
1578 ;; Check-boxes must belong to the tag.
1579 (and tag
(format "[%s] "
1581 (org-export-data tag info
)))))))
1582 (concat counter
"\\item" (or tag
(concat " " checkbox
))
1584 ;; If there are footnotes references in tag, be sure to
1585 ;; add their definition at the end of the item. This
1586 ;; workaround is necessary since "\footnote{}" command is
1587 ;; not supported in tags.
1589 (org-e-latex--delayed-footnotes-definitions
1590 (org-element-property :tag item
) info
)))))
1595 (defun org-e-latex-keyword (keyword contents info
)
1596 "Transcode a KEYWORD element from Org to LaTeX.
1597 CONTENTS is nil. INFO is a plist holding contextual information."
1598 (let ((key (org-element-property :key keyword
))
1599 (value (org-element-property :value keyword
)))
1601 ((string= key
"LATEX") value
)
1602 ((string= key
"INDEX") (format "\\index{%s}" value
))
1603 ;; Invisible targets.
1604 ((string= key
"TARGET") nil
)
1605 ((string= key
"TOC")
1606 (let ((value (downcase value
)))
1608 ((string-match "\\<headlines\\>" value
)
1609 (let ((depth (or (and (string-match "[0-9]+" value
)
1610 (string-to-number (match-string 0 value
)))
1611 (plist-get info
:with-toc
))))
1613 (when (wholenump depth
)
1614 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1615 "\\tableofcontents")))
1616 ((string= "tables" value
) "\\listoftables")
1617 ((string= "figures" value
) "\\listoffigures")
1618 ((string= "listings" value
)
1620 ((eq org-e-latex-listings
'minted
) "\\listoflistings")
1621 (org-e-latex-listings "\\lstlistoflistings")
1622 ;; At the moment, src blocks with a caption are wrapped
1623 ;; into a figure environment.
1624 (t "\\listoffigures")))))))))
1627 ;;;; Latex Environment
1629 (defun org-e-latex-latex-environment (latex-environment contents info
)
1630 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1631 CONTENTS is nil. INFO is a plist holding contextual information."
1632 (let ((label (org-element-property :name latex-environment
))
1633 (value (org-remove-indentation
1634 (org-element-property :value latex-environment
))))
1635 (if (not (org-string-nw-p label
)) value
1636 ;; Environment is labelled: label must be within the environment
1637 ;; (otherwise, a reference pointing to that element will count
1638 ;; the section instead).
1641 (goto-char (point-min))
1643 (insert (format "\\label{%s}\n" label
))
1649 (defun org-e-latex-latex-fragment (latex-fragment contents info
)
1650 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1651 CONTENTS is nil. INFO is a plist holding contextual information."
1652 (org-element-property :value latex-fragment
))
1657 (defun org-e-latex-line-break (line-break contents info
)
1658 "Transcode a LINE-BREAK object from Org to LaTeX.
1659 CONTENTS is nil. INFO is a plist holding contextual information."
1665 (defun org-e-latex-link--inline-image (link info
)
1666 "Return LaTeX code for an inline image.
1667 LINK is the link pointing to the inline image. INFO is a plist
1668 used as a communication channel."
1669 (let* ((parent (org-export-get-parent-element link
))
1670 (path (let ((raw-path (org-element-property :path link
)))
1671 (if (not (file-name-absolute-p raw-path
)) raw-path
1672 (expand-file-name raw-path
))))
1673 (caption (org-e-latex--caption/label-string
1674 (org-element-property :caption parent
)
1675 (org-element-property :name parent
)
1677 ;; Retrieve latex attributes from the element around.
1678 (attr (let ((raw-attr
1679 (mapconcat #'identity
1680 (org-element-property :attr_latex parent
)
1682 (unless (string= raw-attr
"") raw-attr
)))
1685 ((and attr
(string-match "\\<wrap\\>" attr
)) 'wrap
)
1686 ((and attr
(string-match "\\<multicolumn\\>" attr
)) 'multicolumn
)
1687 ((or (and attr
(string-match "\\<float\\>" attr
))
1688 (not (string= caption
"")))
1692 ((and attr
(string-match "\\<placement=\\(\\S-+\\)" attr
))
1693 (org-match-string-no-properties 1 attr
))
1694 ((eq disposition
'wrap
) "{l}{0.5\\textwidth}")
1695 ((eq disposition
'float
)
1696 (concat "[" org-e-latex-default-figure-position
"]"))
1698 ;; Now clear ATTR from any special keyword and set a default
1699 ;; value if nothing is left.
1703 (replace-regexp-in-string
1704 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr
))))
1705 (setq attr
(cond ((not (string= attr
"")) attr
)
1706 ((eq disposition
'float
) "width=0.7\\textwidth")
1707 ((eq disposition
'wrap
) "width=0.48\\textwidth")
1708 (t (or org-e-latex-image-default-option
""))))
1709 ;; Return proper string, depending on DISPOSITION.
1711 (wrap (format "\\begin{wrapfigure}%s
1713 \\includegraphics[%s]{%s}
1714 %s\\end{wrapfigure}" placement attr path caption
))
1715 (multicolumn (format "\\begin{figure*}%s
1717 \\includegraphics[%s]{%s}
1718 %s\\end{figure*}" placement attr path caption
))
1719 (float (format "\\begin{figure}%s
1721 \\includegraphics[%s]{%s}
1722 %s\\end{figure}" placement attr path caption
))
1723 (t (format "\\includegraphics[%s]{%s}" attr path
)))))
1725 (defun org-e-latex-link (link desc info
)
1726 "Transcode a LINK object from Org to LaTeX.
1728 DESC is the description part of the link, or the empty string.
1729 INFO is a plist holding contextual information. See
1731 (let* ((type (org-element-property :type link
))
1732 (raw-path (org-element-property :path link
))
1733 ;; Ensure DESC really exists, or set it to nil.
1734 (desc (and (not (string= desc
"")) desc
))
1735 (imagep (org-export-inline-image-p
1736 link org-e-latex-inline-image-rules
))
1738 ((member type
'("http" "https" "ftp" "mailto"))
1739 (concat type
":" raw-path
))
1740 ((string= type
"file")
1741 (when (string-match "\\(.+\\)::.+" raw-path
)
1742 (setq raw-path
(match-string 1 raw-path
)))
1743 (if (file-name-absolute-p raw-path
)
1744 (concat "file://" (expand-file-name raw-path
))
1745 (concat "file://" raw-path
)))
1750 (imagep (org-e-latex-link--inline-image link info
))
1751 ;; Radio link: Transcode target's contents and use them as link's
1753 ((string= type
"radio")
1754 (let ((destination (org-export-resolve-radio-link link info
)))
1756 (format "\\hyperref[%s]{%s}"
1757 (org-export-solidify-link-text path
)
1758 (org-export-data (org-element-contents destination
) info
)))))
1759 ;; Links pointing to an headline: Find destination and build
1760 ;; appropriate referencing command.
1761 ((member type
'("custom-id" "fuzzy" "id"))
1762 (let ((destination (if (string= type
"fuzzy")
1763 (org-export-resolve-fuzzy-link link info
)
1764 (org-export-resolve-id-link link info
))))
1765 (case (org-element-type destination
)
1766 ;; Id link points to an external file.
1768 (if desc
(format "\\href{file://%s}{%s}" destination desc
)
1769 (format "\\url{file://%s}" destination
)))
1770 ;; Fuzzy link points nowhere.
1772 (format org-e-latex-link-with-unknown-path-format
1775 (org-element-property :raw-link link
) info
))))
1776 ;; Fuzzy link points to an invisible target.
1778 ;; LINK points to an headline. If headlines are numbered
1779 ;; and the link has no description, display headline's
1780 ;; number. Otherwise, display description or headline's
1787 (org-export-get-headline-number destination info
)
1789 (if (and (plist-get info
:section-numbers
) (not desc
))
1790 (format "\\ref{%s}" label
)
1791 (format "\\hyperref[%s]{%s}" label
1794 (org-element-property :title destination
) info
))))))
1795 ;; Fuzzy link points to a target. Do as above.
1797 (let ((path (org-export-solidify-link-text path
)))
1798 (if (not desc
) (format "\\ref{%s}" path
)
1799 (format "\\hyperref[%s]{%s}" path desc
)))))))
1800 ;; Coderef: replace link with the reference name or the
1801 ;; equivalent line number.
1802 ((string= type
"coderef")
1803 (format (org-export-get-coderef-format path desc
)
1804 (org-export-resolve-coderef path info
)))
1805 ;; Link type is handled by a special function.
1806 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1807 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1808 ;; External link with a description part.
1809 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1810 ;; External link without a description part.
1811 (path (format "\\url{%s}" path
))
1812 ;; No path, only description. Try to do something useful.
1813 (t (format org-e-latex-link-with-unknown-path-format desc
)))))
1818 (defun org-e-latex-macro (macro contents info
)
1819 "Transcode a MACRO element from Org to LaTeX.
1820 CONTENTS is nil. INFO is a plist holding contextual information."
1821 ;; Use available tools.
1822 (org-export-expand-macro macro info
))
1827 (defun org-e-latex-paragraph (paragraph contents info
)
1828 "Transcode a PARAGRAPH element from Org to LaTeX.
1829 CONTENTS is the contents of the paragraph, as a string. INFO is
1830 the plist used as a communication channel."
1836 (defun org-e-latex-plain-list (plain-list contents info
)
1837 "Transcode a PLAIN-LIST element from Org to LaTeX.
1838 CONTENTS is the contents of the list. INFO is a plist holding
1839 contextual information."
1840 (let* ((type (org-element-property :type plain-list
))
1841 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1842 "inparadesc" "asparadesc"))
1843 (paralist-regexp (concat
1845 (mapconcat 'identity paralist-types
"\\|")
1847 (attr (mapconcat #'identity
1848 (org-element-property :attr_latex plain-list
)
1853 (format "\\<%s\\>" paralist-regexp
) attr
))
1854 (match-string 1 attr
))
1855 ((eq type
'ordered
) "enumerate")
1856 ((eq type
'unordered
) "itemize")
1857 ((eq type
'descriptive
) "description"))))
1858 (org-e-latex--wrap-label
1860 (format "\\begin{%s}%s\n%s\\end{%s}"
1862 ;; Once special environment, if any, has been removed, the
1863 ;; rest of the attributes will be optional arguments.
1864 ;; They will be put inside square brackets if necessary.
1865 (let ((opt (replace-regexp-in-string
1866 (format " *%s *" paralist-regexp
) "" attr
)))
1867 (cond ((string= opt
"") "")
1868 ((string-match "\\`\\[[^][]+\\]\\'" opt
) opt
)
1869 (t (format "[%s]" opt
))))
1876 (defun org-e-latex-plain-text (text info
)
1877 "Transcode a TEXT string from Org to LaTeX.
1878 TEXT is the string to transcode. INFO is a plist holding
1879 contextual information."
1880 ;; Protect %, #, &, $, ~, ^, _, { and }.
1881 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text
)
1883 (replace-match (format "\\%s" (match-string 2 text
)) nil t text
2)))
1885 (setq text
(replace-regexp-in-string
1886 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1887 "$\\backslash$" text nil t
1))
1888 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1889 (let ((case-fold-search nil
)
1891 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start
)
1892 (setq text
(replace-match
1893 (format "\\%s{}" (match-string 1 text
)) nil t text
)
1894 start
(match-end 0))))
1895 ;; Handle quotation marks
1896 (setq text
(org-e-latex--quotation-marks text info
))
1897 ;; Convert special strings.
1898 (when (plist-get info
:with-special-strings
)
1899 (while (string-match (regexp-quote "...") text
)
1900 (setq text
(replace-match "\\ldots{}" nil t text
))))
1901 ;; Handle break preservation if required.
1902 (when (plist-get info
:preserve-breaks
)
1903 (setq text
(replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1911 (defun org-e-latex-planning (planning contents info
)
1912 "Transcode a PLANNING element from Org to LaTeX.
1913 CONTENTS is nil. INFO is a plist holding contextual
1921 (let ((closed (org-element-property :closed planning
)))
1924 (format "\\textbf{%s} " org-closed-string
)
1925 (format org-e-latex-inactive-timestamp-format
1926 (org-translate-time closed
)))))
1927 (let ((deadline (org-element-property :deadline planning
)))
1930 (format "\\textbf{%s} " org-deadline-string
)
1931 (format org-e-latex-active-timestamp-format
1932 (org-translate-time deadline
)))))
1933 (let ((scheduled (org-element-property :scheduled planning
)))
1936 (format "\\textbf{%s} " org-scheduled-string
)
1937 (format org-e-latex-active-timestamp-format
1938 (org-translate-time scheduled
)))))))
1943 ;;;; Property Drawer
1945 (defun org-e-latex-property-drawer (property-drawer contents info
)
1946 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1947 CONTENTS is nil. INFO is a plist holding contextual
1949 ;; The property drawer isn't exported but we want separating blank
1950 ;; lines nonetheless.
1956 (defun org-e-latex-quote-block (quote-block contents info
)
1957 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1958 CONTENTS holds the contents of the block. INFO is a plist
1959 holding contextual information."
1960 (org-e-latex--wrap-label
1962 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
1967 (defun org-e-latex-quote-section (quote-section contents info
)
1968 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1969 CONTENTS is nil. INFO is a plist holding contextual information."
1970 (let ((value (org-remove-indentation
1971 (org-element-property :value quote-section
))))
1972 (when value
(format "\\begin{verbatim}\n%s\\end{verbatim}" value
))))
1977 (defun org-e-latex-radio-target (radio-target text info
)
1978 "Transcode a RADIO-TARGET object from Org to LaTeX.
1979 TEXT is the text of the target. INFO is a plist holding
1980 contextual information."
1981 (format "\\label{%s}%s"
1982 (org-export-solidify-link-text
1983 (org-element-property :value radio-target
))
1989 (defun org-e-latex-section (section contents info
)
1990 "Transcode a SECTION element from Org to LaTeX.
1991 CONTENTS holds the contents of the section. INFO is a plist
1992 holding contextual information."
1998 (defun org-e-latex-special-block (special-block contents info
)
1999 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2000 CONTENTS holds the contents of the block. INFO is a plist
2001 holding contextual information."
2002 (let ((type (downcase (org-element-property :type special-block
))))
2003 (org-e-latex--wrap-label
2005 (format "\\begin{%s}\n%s\\end{%s}" type contents type
))))
2010 (defun org-e-latex-src-block (src-block contents info
)
2011 "Transcode a SRC-BLOCK element from Org to LaTeX.
2012 CONTENTS holds the contents of the item. INFO is a plist holding
2013 contextual information."
2014 (let* ((lang (org-element-property :language src-block
))
2015 (caption (org-element-property :caption src-block
))
2016 (label (org-element-property :name src-block
))
2017 (custom-env (and lang
2018 (cadr (assq (intern lang
)
2019 org-e-latex-custom-lang-environments
))))
2020 (num-start (case (org-element-property :number-lines src-block
)
2021 (continued (org-export-get-loc src-block info
))
2023 (retain-labels (org-element-property :retain-labels src-block
)))
2025 ;; Case 1. No source fontification.
2026 ((not org-e-latex-listings
)
2027 (let ((caption-str (org-e-latex--caption/label-string caption label info
))
2028 (float-env (when caption
"\\begin{figure}[H]\n%s\n\\end{figure}")))
2032 (format "\\begin{verbatim}\n%s\\end{verbatim}"
2033 (org-export-format-code-default src-block info
))))))
2034 ;; Case 2. Custom environment.
2035 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2037 (org-export-format-code-default src-block info
)
2039 ;; Case 3. Use minted package.
2040 ((eq org-e-latex-listings
'minted
)
2041 (let ((float-env (when (or label caption
)
2042 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2043 (org-e-latex--caption/label-string
2044 caption label info
))))
2047 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2049 (org-e-latex--make-option-string
2050 (if (not num-start
) org-e-latex-minted-options
2051 (append `(("linenos")
2052 ("firstnumber" ,(number-to-string (1+ num-start
))))
2053 org-e-latex-minted-options
)))
2055 (or (cadr (assq (intern lang
) org-e-latex-minted-langs
)) lang
)
2057 (let* ((code-info (org-export-unravel-code src-block
))
2061 (org-split-string (car code-info
) "\n")))))
2062 (org-export-format-code
2064 (lambda (loc num ref
)
2068 ;; Ensure references are flushed to the right,
2069 ;; separated with 6 spaces from the widest line
2071 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2072 (format "(%s)" ref
)))))
2073 nil
(and retain-labels
(cdr code-info
)))))))
2075 (if float-env
(format float-env body
) body
)))
2076 ;; Case 4. Use listings package.
2079 (or (cadr (assq (intern lang
) org-e-latex-listings-langs
)) lang
))
2082 (let ((main (org-export-data (car caption
) info
)))
2083 (if (not (cdr caption
)) (format "{%s}" main
)
2085 (org-export-data (cdr caption
) info
)
2089 (format "\\lstset{%s}\n"
2090 (org-e-latex--make-option-string
2091 (append org-e-latex-listings-options
2092 `(("language" ,lst-lang
))
2093 (when label
`(("label" ,label
)))
2094 (when caption-str
`(("caption" ,caption-str
)))
2095 (cond ((not num-start
) '(("numbers" "none")))
2096 ((zerop num-start
) '(("numbers" "left")))
2097 (t `(("numbers" "left")
2099 ,(number-to-string (1+ num-start
)))))))))
2102 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2103 (let* ((code-info (org-export-unravel-code src-block
))
2107 (org-split-string (car code-info
) "\n")))))
2108 (org-export-format-code
2110 (lambda (loc num ref
)
2114 ;; Ensure references are flushed to the right,
2115 ;; separated with 6 spaces from the widest line of
2117 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2118 (format "(%s)" ref
)))))
2119 nil
(and retain-labels
(cdr code-info
)))))))))))
2122 ;;;; Statistics Cookie
2124 (defun org-e-latex-statistics-cookie (statistics-cookie contents info
)
2125 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2126 CONTENTS is nil. INFO is a plist holding contextual information."
2127 (org-element-property :value statistics-cookie
))
2132 (defun org-e-latex-strike-through (strike-through contents info
)
2133 "Transcode STRIKE-THROUGH from Org to LaTeX.
2134 CONTENTS is the text with strike-through markup. INFO is a plist
2135 holding contextual information."
2136 (org-e-latex--text-markup contents
'strike-through
))
2141 (defun org-e-latex-subscript (subscript contents info
)
2142 "Transcode a SUBSCRIPT object from Org to LaTeX.
2143 CONTENTS is the contents of the object. INFO is a plist holding
2144 contextual information."
2145 (if (= (length contents
) 1) (format "$_%s$" contents
)
2146 ;; Handle multiple objects in SUBSCRIPT by creating a subscript
2147 ;; command for each of them.
2148 (let ((prev-blanks 0))
2151 (case (org-element-type obj
)
2152 ((entity latex-fragment
)
2153 (setq prev-blanks
(org-element-property :post-blank obj
))
2154 (let ((data (org-trim (org-export-data obj info
))))
2156 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2158 (format "$_{%s}$" (match-string 1 data
))))
2160 (format "$_\\mathrm{%s}$"
2161 (concat (make-string prev-blanks ?
)
2162 ;; mathrm command doesn't handle spaces,
2163 ;; so we have to enforce them.
2164 (replace-regexp-in-string
2165 " " "\\\\ " (org-export-data obj info
)))))
2167 (setq prev-blanks
(org-element-property :post-blank obj
))
2168 (format "$_{%s}$" (org-export-data obj info
)))))
2169 (org-element-contents subscript
) ""))))
2174 (defun org-e-latex-superscript (superscript contents info
)
2175 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2176 CONTENTS is the contents of the object. INFO is a plist holding
2177 contextual information."
2178 (if (= (length contents
) 1) (format "$^%s$" contents
)
2179 ;; Handle multiple objects in SUPERSCRIPT by creating
2180 ;; a superscript command for each of them.
2181 (let ((prev-blanks 0))
2184 (case (org-element-type obj
)
2185 ((entity latex-fragment
)
2186 (setq prev-blanks
(org-element-property :post-blank obj
))
2187 (let ((data (org-trim (org-export-data obj info
))))
2189 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2191 (format "$^{%s}$" (match-string 1 data
))))
2193 (format "$^\\mathrm{%s}$"
2194 (concat (make-string prev-blanks ?
)
2195 ;; mathrm command doesn't handle spaces,
2196 ;; so we have to enforce them.
2197 (replace-regexp-in-string
2198 " " "\\\\ " (org-export-data obj info
)))))
2200 (setq prev-blanks
(org-element-property :post-blank obj
))
2201 (format "$^{%s}$" (org-export-data obj info
)))))
2202 (org-element-contents superscript
) ""))))
2207 ;; `org-e-latex-table' is the entry point for table transcoding. It
2208 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
2209 ;; delegates the job to either `org-e-latex-table--table.el-table' or
2210 ;; `org-e-latex-table--org-table' functions, depending of the type of
2213 ;; `org-e-latex-table--align-string' is a subroutine used to build
2214 ;; alignment string for Org tables.
2216 (defun org-e-latex-table (table contents info
)
2217 "Transcode a TABLE element from Org to LaTeX.
2218 CONTENTS is the contents of the table. INFO is a plist holding
2219 contextual information."
2221 ;; Case 1: verbatim table.
2222 ((or org-e-latex-tables-verbatim
2223 (let ((attr (mapconcat 'identity
2224 (org-element-property :attr_latex table
)
2226 (and attr
(string-match "\\<verbatim\\>" attr
))))
2227 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2228 ;; Re-create table, without affiliated keywords.
2230 (org-element-interpret-data
2231 `(table nil
,@(org-element-contents table
))))))
2232 ;; Case 2: table.el table. Convert it using appropriate tools.
2233 ((eq (org-element-property :type table
) 'table.el
)
2234 (org-e-latex-table--table.el-table table contents info
))
2235 ;; Case 3: Standard table.
2236 (t (org-e-latex-table--org-table table contents info
))))
2238 (defun org-e-latex-table--align-string (table info
)
2239 "Return an appropriate LaTeX alignment string.
2240 TABLE is the considered table. INFO is a plist used as
2241 a communication channel."
2242 (let ((attr (mapconcat 'identity
2243 (org-element-property :attr_latex table
)
2245 (if (string-match "\\<align=\\(\\S-+\\)" attr
) (match-string 1 attr
)
2247 ;; Extract column groups and alignment from first (non-rule)
2253 (and (eq (org-element-property :type row
) 'standard
) row
))
2257 (let ((borders (org-export-table-cell-borders cell info
)))
2258 ;; Check left border for the first cell only.
2259 (when (and (memq 'left borders
) (not alignment
))
2260 (push "|" alignment
))
2261 (push (case (org-export-table-cell-alignment cell info
)
2266 (when (memq 'right borders
) (push "|" alignment
))))
2268 (apply 'concat
(reverse alignment
))))))
2270 (defun org-e-latex-table--org-table (table contents info
)
2271 "Return appropriate LaTeX code for an Org table.
2273 TABLE is the table type element to transcode. CONTENTS is its
2274 contents, as a string. INFO is a plist used as a communication
2277 This function assumes TABLE has `org' as its `:type' attribute."
2278 (let* ((label (org-element-property :name table
))
2279 (caption (org-e-latex--caption/label-string
2280 (org-element-property :caption table
) label info
))
2281 (attr (mapconcat 'identity
2282 (org-element-property :attr_latex table
)
2284 ;; Determine alignment string.
2285 (alignment (org-e-latex-table--align-string table info
))
2286 ;; Determine environment for the table: longtable, tabular...
2288 ((not attr
) org-e-latex-default-table-environment
)
2289 ((string-match "\\<longtable\\>" attr
) "longtable")
2290 ((string-match "\\<tabular.?\\>" attr
)
2291 (org-match-string-no-properties 0 attr
))
2292 (t org-e-latex-default-table-environment
)))
2293 ;; If table is a float, determine environment: table, table*
2294 ;; or sidewaystable.
2296 ((string= "longtable" table-env
) nil
)
2297 ((and attr
(string-match "\\<sidewaystable\\>" attr
))
2300 (or (string-match (regexp-quote "table*") attr
)
2301 (string-match "\\<multicolumn\\>" attr
)))
2303 ((or (not (string= caption
"")) label
) "table")))
2304 ;; Extract others display options.
2305 (width (and attr
(string-match "\\<width=\\(\\S-+\\)" attr
)
2306 (org-match-string-no-properties 1 attr
)))
2308 (if (and attr
(string-match "\\<placement=\\(\\S-+\\)" attr
))
2309 (org-match-string-no-properties 1 attr
)
2310 (format "[%s]" org-e-latex-default-figure-position
))))
2311 ;; Prepare the final format string for the table.
2314 ((string= "longtable" table-env
)
2316 "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
2318 (if (or (not org-e-latex-table-caption-above
) (string= "" caption
)) ""
2319 (concat (org-trim caption
) "\\\\\n"))
2321 (if (or org-e-latex-table-caption-above
(string= "" caption
)) ""
2322 (concat (org-trim caption
) "\\\\\n"))))
2324 (t (concat (when float-env
2326 (format "\\begin{%s}%s\n" float-env placement
)
2327 (if org-e-latex-table-caption-above caption
"")))
2328 (when org-e-latex-tables-centered
"\\begin{center}\n")
2329 (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2331 (if width
(format "{%s}" width
) "")
2335 (when org-e-latex-tables-centered
"\n\\end{center}")
2337 (concat (if org-e-latex-table-caption-above
"" caption
)
2338 (format "\n\\end{%s}" float-env
))))))))
2340 (defun org-e-latex-table--table.el-table
(table contents info
)
2341 "Return appropriate LaTeX code for a table.el table.
2343 TABLE is the table type element to transcode. CONTENTS is its
2344 contents, as a string. INFO is a plist used as a communication
2347 This function assumes TABLE has `table.el' as its `:type'
2350 ;; Ensure "*org-export-table*" buffer is empty.
2351 (with-current-buffer (get-buffer-create "*org-export-table*")
2353 (let ((output (with-temp-buffer
2354 (insert (org-element-property :value table
))
2356 (re-search-forward "^[ \t]*|[^|]" nil t
)
2357 (table-generate-source 'latex
"*org-export-table*")
2358 (with-current-buffer "*org-export-table*"
2359 (org-trim (buffer-string))))))
2360 (kill-buffer (get-buffer "*org-export-table*"))
2361 ;; Remove left out comments.
2362 (while (string-match "^%.*\n" output
)
2363 (setq output
(replace-match "" t t output
)))
2364 ;; When the "rmlines" attribute is provided, remove all hlines but
2365 ;; the the one separating heading from the table body.
2366 (let ((attr (mapconcat 'identity
2367 (org-element-property :attr_latex table
)
2369 (when (and attr
(string-match "\\<rmlines\\>" attr
))
2370 (let ((n 0) (pos 0))
2371 (while (and (< (length output
) pos
)
2372 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2375 (setq output
(replace-match "" nil nil output
)))))))
2376 (if (not org-e-latex-tables-centered
) output
2377 (format "\\begin{center}\n%s\n\\end{center}" output
))))
2382 (defun org-e-latex-table-cell (table-cell contents info
)
2383 "Transcode a TABLE-CELL element from Org to LaTeX.
2384 CONTENTS is the cell contents. INFO is a plist used as
2385 a communication channel."
2386 (concat (if (and contents
2387 org-e-latex-table-scientific-notation
2388 (string-match orgtbl-exp-regexp contents
))
2389 ;; Use appropriate format string for scientific
2391 (format org-e-latex-table-scientific-notation
2392 (match-string 1 contents
)
2393 (match-string 2 contents
))
2395 (when (org-export-get-next-element table-cell info
) " & ")))
2400 (defun org-e-latex-table-row (table-row contents info
)
2401 "Transcode a TABLE-ROW element from Org to LaTeX.
2402 CONTENTS is the contents of the row. INFO is a plist used as
2403 a communication channel."
2404 ;; Rules are ignored since table separators are deduced from
2405 ;; borders of the current row.
2406 (when (eq (org-element-property :type table-row
) 'standard
)
2407 (let* ((attr (mapconcat 'identity
2408 (org-element-property
2409 :attr_latex
(org-export-get-parent table-row
))
2411 (longtablep (and attr
(string-match "\\<longtable\\>" attr
)))
2413 (or (and attr
(string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr
))
2414 org-e-latex-tables-booktabs
))
2415 ;; TABLE-ROW's borders are extracted from its first cell.
2417 (org-export-table-cell-borders
2418 (car (org-element-contents table-row
)) info
)))
2420 ;; When BOOKTABS are activated enforce top-rule even when no
2421 ;; hline was specifically marked.
2422 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2423 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2426 ;; Special case for long tables. Define header and footers.
2427 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2430 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2433 (if booktabsp
"\\midrule" "\\hline")
2434 (if booktabsp
"\\midrule" "\\hline")
2435 ;; Number of columns.
2436 (cdr (org-export-table-dimensions
2437 (org-export-get-parent-table table-row
) info
))))
2438 ;; When BOOKTABS are activated enforce bottom rule even when
2439 ;; no hline was specifically marked.
2440 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2441 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2442 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2447 (defun org-e-latex-target (target contents info
)
2448 "Transcode a TARGET object from Org to LaTeX.
2449 CONTENTS is nil. INFO is a plist holding contextual
2451 (format "\\label{%s}"
2452 (org-export-solidify-link-text (org-element-property :value target
))))
2457 (defun org-e-latex-timestamp (timestamp contents info
)
2458 "Transcode a TIMESTAMP object from Org to LaTeX.
2459 CONTENTS is nil. INFO is a plist holding contextual
2461 (let ((value (org-translate-time (org-element-property :value timestamp
)))
2462 (range-end (org-element-property :range-end timestamp
)))
2463 (case (org-element-property :type timestamp
)
2464 (active (format org-e-latex-active-timestamp-format value
))
2466 (concat (format org-e-latex-active-timestamp-format value
)
2468 (format org-e-latex-active-timestamp-format
2469 (org-translate-time range-end
))))
2470 (inactive (format org-e-latex-inactive-timestamp-format value
))
2472 (concat (format org-e-latex-inactive-timestamp-format value
)
2474 (format org-e-latex-inactive-timestamp-format
2475 (org-translate-time range-end
))))
2476 (otherwise (format org-e-latex-diary-timestamp-format value
)))))
2481 (defun org-e-latex-underline (underline contents info
)
2482 "Transcode UNDERLINE from Org to LaTeX.
2483 CONTENTS is the text with underline markup. INFO is a plist
2484 holding contextual information."
2485 (org-e-latex--text-markup contents
'underline
))
2490 (defun org-e-latex-verbatim (verbatim contents info
)
2491 "Transcode a VERBATIM object from Org to LaTeX.
2492 CONTENTS is nil. INFO is a plist used as a communication
2494 (org-e-latex--text-markup (org-element-property :value verbatim
) 'verbatim
))
2499 (defun org-e-latex-verse-block (verse-block contents info
)
2500 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2501 CONTENTS is verse block contents. INFO is a plist holding
2502 contextual information."
2503 (org-e-latex--wrap-label
2505 ;; In a verse environment, add a line break to each newline
2506 ;; character and change each white space at beginning of a line
2507 ;; into a space of 1 em. Also change each blank line with
2508 ;; a vertical space of 1 em.
2510 (setq contents
(replace-regexp-in-string
2511 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2512 (replace-regexp-in-string
2513 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
2514 (while (string-match "^[ \t]+" contents
)
2515 (let ((new-str (format "\\hspace*{%dem}"
2516 (length (match-string 0 contents
)))))
2517 (setq contents
(replace-match new-str nil t contents
))))
2518 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
2522 ;;; Interactive functions
2525 (defun org-e-latex-export-as-latex
2526 (&optional subtreep visible-only body-only ext-plist
)
2527 "Export current buffer as a LaTeX buffer.
2529 If narrowing is active in the current buffer, only export its
2532 If a region is active, export that region.
2534 When optional argument SUBTREEP is non-nil, export the sub-tree
2535 at point, extracting information from the headline properties
2538 When optional argument VISIBLE-ONLY is non-nil, don't export
2539 contents of hidden elements.
2541 When optional argument BODY-ONLY is non-nil, only write code
2542 between \"\\begin{document}\" and \"\\end{document}\".
2544 EXT-PLIST, when provided, is a property list with external
2545 parameters overriding Org default settings, but still inferior to
2546 file-local settings.
2548 Export is done in a buffer named \"*Org E-LATEX Export*\", which
2549 will be displayed when `org-export-show-temporary-export-buffer'
2552 (let ((outbuf (org-export-to-buffer
2553 'e-latex
"*Org E-LATEX Export*"
2554 subtreep visible-only body-only ext-plist
)))
2555 (with-current-buffer outbuf
(LaTeX-mode))
2556 (when org-export-show-temporary-export-buffer
2557 (switch-to-buffer-other-window outbuf
))))
2560 (defun org-e-latex-export-to-latex
2561 (&optional subtreep visible-only body-only ext-plist pub-dir
)
2562 "Export current buffer to a LaTeX file.
2564 If narrowing is active in the current buffer, only export its
2567 If a region is active, export that region.
2569 When optional argument SUBTREEP is non-nil, export the sub-tree
2570 at point, extracting information from the headline properties
2573 When optional argument VISIBLE-ONLY is non-nil, don't export
2574 contents of hidden elements.
2576 When optional argument BODY-ONLY is non-nil, only write code
2577 between \"\\begin{document}\" and \"\\end{document}\".
2579 EXT-PLIST, when provided, is a property list with external
2580 parameters overriding Org default settings, but still inferior to
2581 file-local settings.
2583 When optional argument PUB-DIR is set, use it as the publishing
2586 Return output file's name."
2588 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir
)))
2590 'e-latex outfile subtreep visible-only body-only ext-plist
)))
2593 (defun org-e-latex-export-to-pdf
2594 (&optional subtreep visible-only body-only ext-plist pub-dir
)
2595 "Export current buffer to LaTeX then process through to PDF.
2597 If narrowing is active in the current buffer, only export its
2600 If a region is active, export that region.
2602 When optional argument SUBTREEP is non-nil, export the sub-tree
2603 at point, extracting information from the headline properties
2606 When optional argument VISIBLE-ONLY is non-nil, don't export
2607 contents of hidden elements.
2609 When optional argument BODY-ONLY is non-nil, only write code
2610 between \"\\begin{document}\" and \"\\end{document}\".
2612 EXT-PLIST, when provided, is a property list with external
2613 parameters overriding Org default settings, but still inferior to
2614 file-local settings.
2616 When optional argument PUB-DIR is set, use it as the publishing
2619 Return PDF file's name."
2621 (org-e-latex-compile
2622 (org-e-latex-export-to-latex
2623 subtreep visible-only body-only ext-plist pub-dir
)))
2625 (defun org-e-latex-compile (texfile)
2626 "Compile a TeX file.
2628 TEXFILE is the name of the file being compiled. Processing is
2629 done through the command specified in `org-e-latex-pdf-process'.
2631 Return PDF file name or an error if it couldn't be produced."
2632 (let* ((wconfig (current-window-configuration))
2633 (texfile (file-truename texfile
))
2634 (base (file-name-sans-extension texfile
))
2636 (message (format "Processing LaTeX file %s ..." texfile
))
2640 ;; A function is provided: Apply it.
2641 ((functionp org-e-latex-pdf-process
)
2642 (funcall org-e-latex-pdf-process
(shell-quote-argument texfile
)))
2643 ;; A list is provided: Replace %b, %f and %o with appropriate
2644 ;; values in each command before applying it. Output is
2645 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2646 ((consp org-e-latex-pdf-process
)
2647 (let* ((out-dir (or (file-name-directory texfile
) "./"))
2648 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2652 (replace-regexp-in-string
2653 "%b" (shell-quote-argument base
)
2654 (replace-regexp-in-string
2655 "%f" (shell-quote-argument texfile
)
2656 (replace-regexp-in-string
2657 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
2659 org-e-latex-pdf-process
)
2660 ;; Collect standard errors from output buffer.
2661 (setq errors
(org-e-latex--collect-errors outbuf
))))
2662 (t (error "No valid command to process to PDF")))
2663 (let ((pdffile (concat base
".pdf")))
2664 ;; Check for process failure. Provide collected errors if
2666 (if (not (file-exists-p pdffile
))
2667 (error (concat (format "PDF file %s wasn't produced" pdffile
)
2668 (when errors
(concat ": " errors
))))
2669 ;; Else remove log files, when specified, and signal end of
2670 ;; process to user, along with any error encountered.
2671 (when org-e-latex-remove-logfiles
2672 (dolist (ext org-e-latex-logfiles-extensions
)
2673 (let ((file (concat base
"." ext
)))
2674 (when (file-exists-p file
) (delete-file file
)))))
2675 (message (concat "Process completed"
2676 (if (not errors
) "."
2677 (concat " with errors: " errors
)))))
2678 ;; Return output file name.
2680 (set-window-configuration wconfig
))))
2682 (defun org-e-latex--collect-errors (buffer)
2683 "Collect some kind of errors from \"pdflatex\" command output.
2685 BUFFER is the buffer containing output.
2687 Return collected error types as a string, or nil if there was
2689 (with-current-buffer buffer
2691 (goto-char (point-max))
2692 ;; Find final "pdflatex" run.
2693 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t
)
2694 (let ((case-fold-search t
)
2696 (when (save-excursion
2697 (re-search-forward "Reference.*?undefined" nil t
))
2698 (setq errors
(concat errors
" [undefined reference]")))
2699 (when (save-excursion
2700 (re-search-forward "Citation.*?undefined" nil t
))
2701 (setq errors
(concat errors
" [undefined citation]")))
2702 (when (save-excursion
2703 (re-search-forward "Undefined control sequence" nil t
))
2704 (setq errors
(concat errors
" [undefined control sequence]")))
2705 (when (save-excursion
2706 (re-search-forward "^! LaTeX.*?Error" nil t
))
2707 (setq errors
(concat errors
" [LaTeX error]")))
2708 (when (save-excursion
2709 (re-search-forward "^! Package.*?Error" nil t
))
2710 (setq errors
(concat errors
" [package error]")))
2711 (and (org-string-nw-p errors
) (org-trim errors
)))))))
2714 (provide 'org-e-latex
)
2715 ;;; org-e-latex.el ends here