1 ;;; org-e-latex.el --- LaTeX Back-End For Org Export Engine
3 ;; Copyright (C) 2011-2013 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 function 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 the 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 (org-export-read-attribute :attr_latex horizontal-rule
))
1457 (prev (org-export-get-previous-element horizontal-rule info
)))
1459 ;; Make sure the rule doesn't start at the end of the current
1460 ;; line by separating it with a blank line from previous element.
1462 (let ((prev-blank (org-element-property :post-blank prev
)))
1463 (or (not prev-blank
) (zerop prev-blank
))))
1465 (org-e-latex--wrap-label
1467 (format "\\rule{%s}{%s}"
1468 (or (plist-get attr
:width
) "\\linewidth")
1469 (or (plist-get attr
:thickness
) "0.5pt"))))))
1472 ;;;; Inline Babel Call
1474 ;; Inline Babel Calls are ignored.
1477 ;;;; Inline Src Block
1479 (defun org-e-latex-inline-src-block (inline-src-block contents info
)
1480 "Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
1481 CONTENTS holds the contents of the item. INFO is a plist holding
1482 contextual information."
1483 (let* ((code (org-element-property :value inline-src-block
))
1484 (separator (org-e-latex--find-verb-separator code
)))
1486 ;; Do not use a special package: transcode it verbatim.
1487 ((not org-e-latex-listings
)
1488 (concat "\\verb" separator code separator
))
1489 ;; Use minted package.
1490 ((eq org-e-latex-listings
'minted
)
1491 (let* ((org-lang (org-element-property :language inline-src-block
))
1492 (mint-lang (or (cadr (assq (intern org-lang
)
1493 org-e-latex-minted-langs
))
1495 (options (org-e-latex--make-option-string
1496 org-e-latex-minted-options
)))
1497 (concat (format "\\mint%s{%s}"
1498 (if (string= options
"") "" (format "[%s]" options
))
1500 separator code separator
)))
1501 ;; Use listings package.
1503 ;; Maybe translate language's name.
1504 (let* ((org-lang (org-element-property :language inline-src-block
))
1505 (lst-lang (or (cadr (assq (intern org-lang
)
1506 org-e-latex-listings-langs
))
1508 (options (org-e-latex--make-option-string
1509 (append org-e-latex-listings-options
1510 `(("language" ,lst-lang
))))))
1511 (concat (format "\\lstinline[%s]" options
)
1512 separator code separator
))))))
1517 (defun org-e-latex-inlinetask (inlinetask contents info
)
1518 "Transcode an INLINETASK element from Org to LaTeX.
1519 CONTENTS holds the contents of the block. INFO is a plist
1520 holding contextual information."
1521 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1522 (todo (and (plist-get info
:with-todo-keywords
)
1523 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1524 (and todo
(org-export-data todo info
)))))
1525 (todo-type (org-element-property :todo-type inlinetask
))
1526 (tags (and (plist-get info
:with-tags
)
1527 (org-export-get-tags inlinetask info
)))
1528 (priority (and (plist-get info
:with-priority
)
1529 (org-element-property :priority inlinetask
))))
1530 ;; If `org-e-latex-format-inlinetask-function' is provided, call it
1531 ;; with appropriate arguments.
1532 (if (functionp org-e-latex-format-inlinetask-function
)
1533 (funcall org-e-latex-format-inlinetask-function
1534 todo todo-type priority title tags contents
)
1535 ;; Otherwise, use a default template.
1536 (org-e-latex--wrap-label
1540 (when todo
(format "\\textbf{\\textsf{\\textsc{%s}}} " todo
))
1541 (when priority
(format "\\framebox{\\#%c} " priority
))
1543 (when tags
(format "\\hfill{}\\textsc{:%s:}"
1544 (mapconcat 'identity tags
":"))))))
1545 (format (concat "\\begin{center}\n"
1547 "\\begin{minipage}[c]{.6\\textwidth}\n"
1549 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
1554 full-title contents
))))))
1559 (defun org-e-latex-italic (italic contents info
)
1560 "Transcode ITALIC from Org to LaTeX.
1561 CONTENTS is the text with italic markup. INFO is a plist holding
1562 contextual information."
1563 (org-e-latex--text-markup contents
'italic
))
1568 (defun org-e-latex-item (item contents info
)
1569 "Transcode an ITEM element from Org to LaTeX.
1570 CONTENTS holds the contents of the item. INFO is a plist holding
1571 contextual information."
1573 (let ((count (org-element-property :counter item
))
1575 (loop for parent in
(org-export-get-genealogy item
)
1576 count
(eq (org-element-type parent
) 'plain-list
)
1577 until
(eq (org-element-type parent
) 'headline
))))
1580 (format "\\setcounter{enum%s}{%s}\n"
1581 (nth (1- level
) '("i" "ii" "iii" "iv"))
1583 (checkbox (case (org-element-property :checkbox item
)
1584 (on "$\\boxtimes$ ")
1586 (trans "$\\boxminus$ ")))
1587 (tag (let ((tag (org-element-property :tag item
)))
1588 ;; Check-boxes must belong to the tag.
1589 (and tag
(format "[%s] "
1591 (org-export-data tag info
)))))))
1592 (concat counter
"\\item" (or tag
(concat " " checkbox
))
1593 (and contents
(org-trim contents
))
1594 ;; If there are footnotes references in tag, be sure to
1595 ;; add their definition at the end of the item. This
1596 ;; workaround is necessary since "\footnote{}" command is
1597 ;; not supported in tags.
1599 (org-e-latex--delayed-footnotes-definitions
1600 (org-element-property :tag item
) info
)))))
1605 (defun org-e-latex-keyword (keyword contents info
)
1606 "Transcode a KEYWORD element from Org to LaTeX.
1607 CONTENTS is nil. INFO is a plist holding contextual information."
1608 (let ((key (org-element-property :key keyword
))
1609 (value (org-element-property :value keyword
)))
1611 ((string= key
"LATEX") value
)
1612 ((string= key
"INDEX") (format "\\index{%s}" value
))
1613 ;; Invisible targets.
1614 ((string= key
"TARGET") nil
)
1615 ((string= key
"TOC")
1616 (let ((value (downcase value
)))
1618 ((string-match "\\<headlines\\>" value
)
1619 (let ((depth (or (and (string-match "[0-9]+" value
)
1620 (string-to-number (match-string 0 value
)))
1621 (plist-get info
:with-toc
))))
1623 (when (wholenump depth
)
1624 (format "\\setcounter{tocdepth}{%s}\n" depth
))
1625 "\\tableofcontents")))
1626 ((string= "tables" value
) "\\listoftables")
1627 ((string= "figures" value
) "\\listoffigures")
1628 ((string= "listings" value
)
1630 ((eq org-e-latex-listings
'minted
) "\\listoflistings")
1631 (org-e-latex-listings "\\lstlistoflistings")
1632 ;; At the moment, src blocks with a caption are wrapped
1633 ;; into a figure environment.
1634 (t "\\listoffigures")))))))))
1637 ;;;; Latex Environment
1639 (defun org-e-latex-latex-environment (latex-environment contents info
)
1640 "Transcode a LATEX-ENVIRONMENT element from Org to LaTeX.
1641 CONTENTS is nil. INFO is a plist holding contextual information."
1642 (let ((label (org-element-property :name latex-environment
))
1643 (value (org-remove-indentation
1644 (org-element-property :value latex-environment
))))
1645 (if (not (org-string-nw-p label
)) value
1646 ;; Environment is labelled: label must be within the environment
1647 ;; (otherwise, a reference pointing to that element will count
1648 ;; the section instead).
1651 (goto-char (point-min))
1653 (insert (format "\\label{%s}\n" label
))
1659 (defun org-e-latex-latex-fragment (latex-fragment contents info
)
1660 "Transcode a LATEX-FRAGMENT object from Org to LaTeX.
1661 CONTENTS is nil. INFO is a plist holding contextual information."
1662 (org-element-property :value latex-fragment
))
1667 (defun org-e-latex-line-break (line-break contents info
)
1668 "Transcode a LINE-BREAK object from Org to LaTeX.
1669 CONTENTS is nil. INFO is a plist holding contextual information."
1675 (defun org-e-latex-link--inline-image (link info
)
1676 "Return LaTeX code for an inline image.
1677 LINK is the link pointing to the inline image. INFO is a plist
1678 used as a communication channel."
1679 (let* ((parent (org-export-get-parent-element link
))
1680 (path (let ((raw-path (org-element-property :path link
)))
1681 (if (not (file-name-absolute-p raw-path
)) raw-path
1682 (expand-file-name raw-path
))))
1683 (caption (org-e-latex--caption/label-string
1684 (org-element-property :caption parent
)
1685 (org-element-property :name parent
)
1687 ;; Retrieve latex attributes from the element around.
1688 (attr (let ((raw-attr
1689 (mapconcat #'identity
1690 (org-element-property :attr_latex parent
)
1692 (unless (string= raw-attr
"") raw-attr
)))
1695 ((and attr
(string-match "\\<wrap\\>" attr
)) 'wrap
)
1696 ((and attr
(string-match "\\<multicolumn\\>" attr
)) 'multicolumn
)
1697 ((or (and attr
(string-match "\\<float\\>" attr
))
1698 (not (string= caption
"")))
1702 ((and attr
(string-match "\\<placement=\\(\\S-+\\)" attr
))
1703 (org-match-string-no-properties 1 attr
))
1704 ((eq disposition
'wrap
) "{l}{0.5\\textwidth}")
1705 ((eq disposition
'float
)
1706 (concat "[" org-e-latex-default-figure-position
"]"))
1708 ;; Now clear ATTR from any special keyword and set a default
1709 ;; value if nothing is left.
1713 (replace-regexp-in-string
1714 "\\(wrap\\|multicolumn\\|float\\|placement=\\S-+\\)" "" attr
))))
1715 (setq attr
(cond ((not (string= attr
"")) attr
)
1716 ((eq disposition
'float
) "width=0.7\\textwidth")
1717 ((eq disposition
'wrap
) "width=0.48\\textwidth")
1718 (t (or org-e-latex-image-default-option
""))))
1719 ;; Return proper string, depending on DISPOSITION.
1721 (wrap (format "\\begin{wrapfigure}%s
1723 \\includegraphics[%s]{%s}
1724 %s\\end{wrapfigure}" placement attr path caption
))
1725 (multicolumn (format "\\begin{figure*}%s
1727 \\includegraphics[%s]{%s}
1728 %s\\end{figure*}" placement attr path caption
))
1729 (float (format "\\begin{figure}%s
1731 \\includegraphics[%s]{%s}
1732 %s\\end{figure}" placement attr path caption
))
1733 (t (format "\\includegraphics[%s]{%s}" attr path
)))))
1735 (defun org-e-latex-link (link desc info
)
1736 "Transcode a LINK object from Org to LaTeX.
1738 DESC is the description part of the link, or the empty string.
1739 INFO is a plist holding contextual information. See
1741 (let* ((type (org-element-property :type link
))
1742 (raw-path (org-element-property :path link
))
1743 ;; Ensure DESC really exists, or set it to nil.
1744 (desc (and (not (string= desc
"")) desc
))
1745 (imagep (org-export-inline-image-p
1746 link org-e-latex-inline-image-rules
))
1748 ((member type
'("http" "https" "ftp" "mailto"))
1749 (concat type
":" raw-path
))
1750 ((string= type
"file")
1751 (when (string-match "\\(.+\\)::.+" raw-path
)
1752 (setq raw-path
(match-string 1 raw-path
)))
1753 (if (file-name-absolute-p raw-path
)
1754 (concat "file://" (expand-file-name raw-path
))
1755 (concat "file://" raw-path
)))
1760 (imagep (org-e-latex-link--inline-image link info
))
1761 ;; Radio link: Transcode target's contents and use them as link's
1763 ((string= type
"radio")
1764 (let ((destination (org-export-resolve-radio-link link info
)))
1766 (format "\\hyperref[%s]{%s}"
1767 (org-export-solidify-link-text path
)
1768 (org-export-data (org-element-contents destination
) info
)))))
1769 ;; Links pointing to an headline: Find destination and build
1770 ;; appropriate referencing command.
1771 ((member type
'("custom-id" "fuzzy" "id"))
1772 (let ((destination (if (string= type
"fuzzy")
1773 (org-export-resolve-fuzzy-link link info
)
1774 (org-export-resolve-id-link link info
))))
1775 (case (org-element-type destination
)
1776 ;; Id link points to an external file.
1778 (if desc
(format "\\href{file://%s}{%s}" destination desc
)
1779 (format "\\url{file://%s}" destination
)))
1780 ;; Fuzzy link points nowhere.
1782 (format org-e-latex-link-with-unknown-path-format
1785 (org-element-property :raw-link link
) info
))))
1786 ;; Fuzzy link points to an invisible target.
1788 ;; LINK points to an headline. If headlines are numbered
1789 ;; and the link has no description, display headline's
1790 ;; number. Otherwise, display description or headline's
1797 (org-export-get-headline-number destination info
)
1799 (if (and (plist-get info
:section-numbers
) (not desc
))
1800 (format "\\ref{%s}" label
)
1801 (format "\\hyperref[%s]{%s}" label
1804 (org-element-property :title destination
) info
))))))
1805 ;; Fuzzy link points to a target. Do as above.
1807 (let ((path (org-export-solidify-link-text path
)))
1808 (if (not desc
) (format "\\ref{%s}" path
)
1809 (format "\\hyperref[%s]{%s}" path desc
)))))))
1810 ;; Coderef: replace link with the reference name or the
1811 ;; equivalent line number.
1812 ((string= type
"coderef")
1813 (format (org-export-get-coderef-format path desc
)
1814 (org-export-resolve-coderef path info
)))
1815 ;; Link type is handled by a special function.
1816 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
1817 (funcall protocol
(org-link-unescape path
) desc
'latex
))
1818 ;; External link with a description part.
1819 ((and path desc
) (format "\\href{%s}{%s}" path desc
))
1820 ;; External link without a description part.
1821 (path (format "\\url{%s}" path
))
1822 ;; No path, only description. Try to do something useful.
1823 (t (format org-e-latex-link-with-unknown-path-format desc
)))))
1828 (defun org-e-latex-macro (macro contents info
)
1829 "Transcode a MACRO element from Org to LaTeX.
1830 CONTENTS is nil. INFO is a plist holding contextual information."
1831 ;; Use available tools.
1832 (org-export-expand-macro macro info
))
1837 (defun org-e-latex-paragraph (paragraph contents info
)
1838 "Transcode a PARAGRAPH element from Org to LaTeX.
1839 CONTENTS is the contents of the paragraph, as a string. INFO is
1840 the plist used as a communication channel."
1846 (defun org-e-latex-plain-list (plain-list contents info
)
1847 "Transcode a PLAIN-LIST element from Org to LaTeX.
1848 CONTENTS is the contents of the list. INFO is a plist holding
1849 contextual information."
1850 (let* ((type (org-element-property :type plain-list
))
1851 (paralist-types '("inparaenum" "asparaenum" "inparaitem" "asparaitem"
1852 "inparadesc" "asparadesc"))
1853 (paralist-regexp (concat
1855 (mapconcat 'identity paralist-types
"\\|")
1857 (attr (mapconcat #'identity
1858 (org-element-property :attr_latex plain-list
)
1863 (format "\\<%s\\>" paralist-regexp
) attr
))
1864 (match-string 1 attr
))
1865 ((eq type
'ordered
) "enumerate")
1866 ((eq type
'unordered
) "itemize")
1867 ((eq type
'descriptive
) "description"))))
1868 (org-e-latex--wrap-label
1870 (format "\\begin{%s}%s\n%s\\end{%s}"
1872 ;; Once special environment, if any, has been removed, the
1873 ;; rest of the attributes will be optional arguments.
1874 ;; They will be put inside square brackets if necessary.
1875 (let ((opt (replace-regexp-in-string
1876 (format " *%s *" paralist-regexp
) "" attr
)))
1877 (cond ((string= opt
"") "")
1878 ((string-match "\\`\\[[^][]+\\]\\'" opt
) opt
)
1879 (t (format "[%s]" opt
))))
1886 (defun org-e-latex-plain-text (text info
)
1887 "Transcode a TEXT string from Org to LaTeX.
1888 TEXT is the string to transcode. INFO is a plist holding
1889 contextual information."
1890 ;; Protect %, #, &, $, ~, ^, _, { and }.
1891 (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text
)
1893 (replace-match (format "\\%s" (match-string 2 text
)) nil t text
2)))
1895 (setq text
(replace-regexp-in-string
1896 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1897 "$\\backslash$" text nil t
1))
1898 ;; LaTeX into \LaTeX{} and TeX into \TeX{}.
1899 (let ((case-fold-search nil
)
1901 (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start
)
1902 (setq text
(replace-match
1903 (format "\\%s{}" (match-string 1 text
)) nil t text
)
1904 start
(match-end 0))))
1905 ;; Handle quotation marks
1906 (setq text
(org-e-latex--quotation-marks text info
))
1907 ;; Convert special strings.
1908 (when (plist-get info
:with-special-strings
)
1909 (while (string-match (regexp-quote "...") text
)
1910 (setq text
(replace-match "\\ldots{}" nil t text
))))
1911 ;; Handle break preservation if required.
1912 (when (plist-get info
:preserve-breaks
)
1913 (setq text
(replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
1921 (defun org-e-latex-planning (planning contents info
)
1922 "Transcode a PLANNING element from Org to LaTeX.
1923 CONTENTS is nil. INFO is a plist holding contextual
1931 (let ((closed (org-element-property :closed planning
)))
1934 (format "\\textbf{%s} " org-closed-string
)
1935 (format org-e-latex-inactive-timestamp-format
1936 (org-translate-time closed
)))))
1937 (let ((deadline (org-element-property :deadline planning
)))
1940 (format "\\textbf{%s} " org-deadline-string
)
1941 (format org-e-latex-active-timestamp-format
1942 (org-translate-time deadline
)))))
1943 (let ((scheduled (org-element-property :scheduled planning
)))
1946 (format "\\textbf{%s} " org-scheduled-string
)
1947 (format org-e-latex-active-timestamp-format
1948 (org-translate-time scheduled
)))))))
1953 ;;;; Property Drawer
1955 (defun org-e-latex-property-drawer (property-drawer contents info
)
1956 "Transcode a PROPERTY-DRAWER element from Org to LaTeX.
1957 CONTENTS is nil. INFO is a plist holding contextual
1959 ;; The property drawer isn't exported but we want separating blank
1960 ;; lines nonetheless.
1966 (defun org-e-latex-quote-block (quote-block contents info
)
1967 "Transcode a QUOTE-BLOCK element from Org to LaTeX.
1968 CONTENTS holds the contents of the block. INFO is a plist
1969 holding contextual information."
1970 (org-e-latex--wrap-label
1972 (format "\\begin{quote}\n%s\\end{quote}" contents
)))
1977 (defun org-e-latex-quote-section (quote-section contents info
)
1978 "Transcode a QUOTE-SECTION element from Org to LaTeX.
1979 CONTENTS is nil. INFO is a plist holding contextual information."
1980 (let ((value (org-remove-indentation
1981 (org-element-property :value quote-section
))))
1982 (when value
(format "\\begin{verbatim}\n%s\\end{verbatim}" value
))))
1987 (defun org-e-latex-radio-target (radio-target text info
)
1988 "Transcode a RADIO-TARGET object from Org to LaTeX.
1989 TEXT is the text of the target. INFO is a plist holding
1990 contextual information."
1991 (format "\\label{%s}%s"
1992 (org-export-solidify-link-text
1993 (org-element-property :value radio-target
))
1999 (defun org-e-latex-section (section contents info
)
2000 "Transcode a SECTION element from Org to LaTeX.
2001 CONTENTS holds the contents of the section. INFO is a plist
2002 holding contextual information."
2008 (defun org-e-latex-special-block (special-block contents info
)
2009 "Transcode a SPECIAL-BLOCK element from Org to LaTeX.
2010 CONTENTS holds the contents of the block. INFO is a plist
2011 holding contextual information."
2012 (let ((type (downcase (org-element-property :type special-block
))))
2013 (org-e-latex--wrap-label
2015 (format "\\begin{%s}\n%s\\end{%s}" type contents type
))))
2020 (defun org-e-latex-src-block (src-block contents info
)
2021 "Transcode a SRC-BLOCK element from Org to LaTeX.
2022 CONTENTS holds the contents of the item. INFO is a plist holding
2023 contextual information."
2024 (let* ((lang (org-element-property :language src-block
))
2025 (caption (org-element-property :caption src-block
))
2026 (label (org-element-property :name src-block
))
2027 (custom-env (and lang
2028 (cadr (assq (intern lang
)
2029 org-e-latex-custom-lang-environments
))))
2030 (num-start (case (org-element-property :number-lines src-block
)
2031 (continued (org-export-get-loc src-block info
))
2033 (retain-labels (org-element-property :retain-labels src-block
)))
2035 ;; Case 1. No source fontification.
2036 ((not org-e-latex-listings
)
2037 (let ((caption-str (org-e-latex--caption/label-string caption label info
))
2038 (float-env (when caption
"\\begin{figure}[H]\n%s\n\\end{figure}")))
2042 (format "\\begin{verbatim}\n%s\\end{verbatim}"
2043 (org-export-format-code-default src-block info
))))))
2044 ;; Case 2. Custom environment.
2045 (custom-env (format "\\begin{%s}\n%s\\end{%s}\n"
2047 (org-export-format-code-default src-block info
)
2049 ;; Case 3. Use minted package.
2050 ((eq org-e-latex-listings
'minted
)
2051 (let ((float-env (when (or label caption
)
2052 (format "\\begin{listing}[H]\n%%s\n%s\\end{listing}"
2053 (org-e-latex--caption/label-string
2054 caption label info
))))
2057 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2059 (org-e-latex--make-option-string
2060 (if (not num-start
) org-e-latex-minted-options
2061 (append `(("linenos")
2062 ("firstnumber" ,(number-to-string (1+ num-start
))))
2063 org-e-latex-minted-options
)))
2065 (or (cadr (assq (intern lang
) org-e-latex-minted-langs
)) lang
)
2067 (let* ((code-info (org-export-unravel-code src-block
))
2071 (org-split-string (car code-info
) "\n")))))
2072 (org-export-format-code
2074 (lambda (loc num ref
)
2078 ;; Ensure references are flushed to the right,
2079 ;; separated with 6 spaces from the widest line
2081 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2082 (format "(%s)" ref
)))))
2083 nil
(and retain-labels
(cdr code-info
)))))))
2085 (if float-env
(format float-env body
) body
)))
2086 ;; Case 4. Use listings package.
2089 (or (cadr (assq (intern lang
) org-e-latex-listings-langs
)) lang
))
2092 (let ((main (org-export-data (car caption
) info
)))
2093 (if (not (cdr caption
)) (format "{%s}" main
)
2095 (org-export-data (cdr caption
) info
)
2099 (format "\\lstset{%s}\n"
2100 (org-e-latex--make-option-string
2101 (append org-e-latex-listings-options
2102 `(("language" ,lst-lang
))
2103 (when label
`(("label" ,label
)))
2104 (when caption-str
`(("caption" ,caption-str
)))
2105 (cond ((not num-start
) '(("numbers" "none")))
2106 ((zerop num-start
) '(("numbers" "left")))
2107 (t `(("numbers" "left")
2109 ,(number-to-string (1+ num-start
)))))))))
2112 "\\begin{lstlisting}\n%s\\end{lstlisting}"
2113 (let* ((code-info (org-export-unravel-code src-block
))
2117 (org-split-string (car code-info
) "\n")))))
2118 (org-export-format-code
2120 (lambda (loc num ref
)
2124 ;; Ensure references are flushed to the right,
2125 ;; separated with 6 spaces from the widest line of
2127 (concat (make-string (+ (- max-width
(length loc
)) 6) ?
)
2128 (format "(%s)" ref
)))))
2129 nil
(and retain-labels
(cdr code-info
)))))))))))
2132 ;;;; Statistics Cookie
2134 (defun org-e-latex-statistics-cookie (statistics-cookie contents info
)
2135 "Transcode a STATISTICS-COOKIE object from Org to LaTeX.
2136 CONTENTS is nil. INFO is a plist holding contextual information."
2137 (replace-regexp-in-string
2138 "%" "\\%" (org-element-property :value statistics-cookie
) nil t
))
2143 (defun org-e-latex-strike-through (strike-through contents info
)
2144 "Transcode STRIKE-THROUGH from Org to LaTeX.
2145 CONTENTS is the text with strike-through markup. INFO is a plist
2146 holding contextual information."
2147 (org-e-latex--text-markup contents
'strike-through
))
2152 (defun org-e-latex-subscript (subscript contents info
)
2153 "Transcode a SUBSCRIPT object from Org to LaTeX.
2154 CONTENTS is the contents of the object. INFO is a plist holding
2155 contextual information."
2156 (if (= (length contents
) 1) (format "$_%s$" contents
)
2157 ;; Handle multiple objects in SUBSCRIPT by creating a subscript
2158 ;; command for each of them.
2159 (let ((prev-blanks 0))
2162 (case (org-element-type obj
)
2163 ((entity latex-fragment
)
2164 (setq prev-blanks
(org-element-property :post-blank obj
))
2165 (let ((data (org-trim (org-export-data obj info
))))
2167 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2169 (format "$_{%s}$" (match-string 1 data
))))
2171 (format "$_\\mathrm{%s}$"
2172 (concat (make-string prev-blanks ?
)
2173 ;; mathrm command doesn't handle spaces,
2174 ;; so we have to enforce them.
2175 (replace-regexp-in-string
2176 " " "\\\\ " (org-export-data obj info
)))))
2178 (setq prev-blanks
(org-element-property :post-blank obj
))
2179 (format "$_{%s}$" (org-export-data obj info
)))))
2180 (org-element-contents subscript
) ""))))
2185 (defun org-e-latex-superscript (superscript contents info
)
2186 "Transcode a SUPERSCRIPT object from Org to LaTeX.
2187 CONTENTS is the contents of the object. INFO is a plist holding
2188 contextual information."
2189 (if (= (length contents
) 1) (format "$^%s$" contents
)
2190 ;; Handle multiple objects in SUPERSCRIPT by creating
2191 ;; a superscript command for each of them.
2192 (let ((prev-blanks 0))
2195 (case (org-element-type obj
)
2196 ((entity latex-fragment
)
2197 (setq prev-blanks
(org-element-property :post-blank obj
))
2198 (let ((data (org-trim (org-export-data obj info
))))
2200 "\\`\\(?:\\\\[([]\\|\\$+\\)?\\(.*?\\)\\(?:\\\\[])]\\|\\$+\\)?\\'"
2202 (format "$^{%s}$" (match-string 1 data
))))
2204 (format "$^\\mathrm{%s}$"
2205 (concat (make-string prev-blanks ?
)
2206 ;; mathrm command doesn't handle spaces,
2207 ;; so we have to enforce them.
2208 (replace-regexp-in-string
2209 " " "\\\\ " (org-export-data obj info
)))))
2211 (setq prev-blanks
(org-element-property :post-blank obj
))
2212 (format "$^{%s}$" (org-export-data obj info
)))))
2213 (org-element-contents superscript
) ""))))
2218 ;; `org-e-latex-table' is the entry point for table transcoding. It
2219 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
2220 ;; delegates the job to either `org-e-latex-table--table.el-table' or
2221 ;; `org-e-latex-table--org-table' functions, depending of the type of
2224 ;; `org-e-latex-table--align-string' is a subroutine used to build
2225 ;; alignment string for Org tables.
2227 (defun org-e-latex-table (table contents info
)
2228 "Transcode a TABLE element from Org to LaTeX.
2229 CONTENTS is the contents of the table. INFO is a plist holding
2230 contextual information."
2232 ;; Case 1: verbatim table.
2233 ((or org-e-latex-tables-verbatim
2234 (let ((attr (mapconcat 'identity
2235 (org-element-property :attr_latex table
)
2237 (and attr
(string-match "\\<verbatim\\>" attr
))))
2238 (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
2239 ;; Re-create table, without affiliated keywords.
2241 (org-element-interpret-data
2242 `(table nil
,@(org-element-contents table
))))))
2243 ;; Case 2: table.el table. Convert it using appropriate tools.
2244 ((eq (org-element-property :type table
) 'table.el
)
2245 (org-e-latex-table--table.el-table table contents info
))
2246 ;; Case 3: Standard table.
2247 (t (org-e-latex-table--org-table table contents info
))))
2249 (defun org-e-latex-table--align-string (table info
)
2250 "Return an appropriate LaTeX alignment string.
2251 TABLE is the considered table. INFO is a plist used as
2252 a communication channel."
2253 (let ((attr (mapconcat 'identity
2254 (org-element-property :attr_latex table
)
2256 (if (string-match "\\<align=\\(\\S-+\\)" attr
) (match-string 1 attr
)
2258 ;; Extract column groups and alignment from first (non-rule)
2264 (and (eq (org-element-property :type row
) 'standard
) row
))
2268 (let ((borders (org-export-table-cell-borders cell info
)))
2269 ;; Check left border for the first cell only.
2270 (when (and (memq 'left borders
) (not alignment
))
2271 (push "|" alignment
))
2272 (push (case (org-export-table-cell-alignment cell info
)
2277 (when (memq 'right borders
) (push "|" alignment
))))
2279 (apply 'concat
(reverse alignment
))))))
2281 (defun org-e-latex-table--org-table (table contents info
)
2282 "Return appropriate LaTeX code for an Org table.
2284 TABLE is the table type element to transcode. CONTENTS is its
2285 contents, as a string. INFO is a plist used as a communication
2288 This function assumes TABLE has `org' as its `:type' attribute."
2289 (let* ((label (org-element-property :name table
))
2290 (caption (org-e-latex--caption/label-string
2291 (org-element-property :caption table
) label info
))
2292 (attr (mapconcat 'identity
2293 (org-element-property :attr_latex table
)
2295 ;; Determine alignment string.
2296 (alignment (org-e-latex-table--align-string table info
))
2297 ;; Determine environment for the table: longtable, tabular...
2299 ((not attr
) org-e-latex-default-table-environment
)
2300 ((string-match "\\<longtable\\>" attr
) "longtable")
2301 ((string-match "\\<tabular.?\\>" attr
)
2302 (org-match-string-no-properties 0 attr
))
2303 (t org-e-latex-default-table-environment
)))
2304 ;; If table is a float, determine environment: table, table*
2305 ;; or sidewaystable.
2307 ((string= "longtable" table-env
) nil
)
2308 ((and attr
(string-match "\\<sidewaystable\\>" attr
))
2311 (or (string-match (regexp-quote "table*") attr
)
2312 (string-match "\\<multicolumn\\>" attr
)))
2314 ((or (not (string= caption
"")) label
) "table")))
2315 ;; Extract others display options.
2316 (width (and attr
(string-match "\\<width=\\(\\S-+\\)" attr
)
2317 (org-match-string-no-properties 1 attr
)))
2319 (if (and attr
(string-match "\\<placement=\\(\\S-+\\)" attr
))
2320 (org-match-string-no-properties 1 attr
)
2321 (format "[%s]" org-e-latex-default-figure-position
))))
2322 ;; Prepare the final format string for the table.
2325 ((string= "longtable" table-env
)
2327 "\\begin{longtable}{%s}\n%s%s%s\\end{longtable}"
2329 (if (or (not org-e-latex-table-caption-above
) (string= "" caption
)) ""
2330 (concat (org-trim caption
) "\\\\\n"))
2332 (if (or org-e-latex-table-caption-above
(string= "" caption
)) ""
2333 (concat (org-trim caption
) "\\\\\n"))))
2335 (t (concat (when float-env
2337 (format "\\begin{%s}%s\n" float-env placement
)
2338 (if org-e-latex-table-caption-above caption
"")))
2339 (when org-e-latex-tables-centered
"\\begin{center}\n")
2340 (format "\\begin{%s}%s{%s}\n%s\\end{%s}"
2342 (if width
(format "{%s}" width
) "")
2346 (when org-e-latex-tables-centered
"\n\\end{center}")
2348 (concat (if org-e-latex-table-caption-above
"" caption
)
2349 (format "\n\\end{%s}" float-env
))))))))
2351 (defun org-e-latex-table--table.el-table
(table contents info
)
2352 "Return appropriate LaTeX code for a table.el table.
2354 TABLE is the table type element to transcode. CONTENTS is its
2355 contents, as a string. INFO is a plist used as a communication
2358 This function assumes TABLE has `table.el' as its `:type'
2361 ;; Ensure "*org-export-table*" buffer is empty.
2362 (with-current-buffer (get-buffer-create "*org-export-table*")
2364 (let ((output (with-temp-buffer
2365 (insert (org-element-property :value table
))
2367 (re-search-forward "^[ \t]*|[^|]" nil t
)
2368 (table-generate-source 'latex
"*org-export-table*")
2369 (with-current-buffer "*org-export-table*"
2370 (org-trim (buffer-string))))))
2371 (kill-buffer (get-buffer "*org-export-table*"))
2372 ;; Remove left out comments.
2373 (while (string-match "^%.*\n" output
)
2374 (setq output
(replace-match "" t t output
)))
2375 ;; When the "rmlines" attribute is provided, remove all hlines but
2376 ;; the the one separating heading from the table body.
2377 (let ((attr (mapconcat 'identity
2378 (org-element-property :attr_latex table
)
2380 (when (and attr
(string-match "\\<rmlines\\>" attr
))
2381 (let ((n 0) (pos 0))
2382 (while (and (< (length output
) pos
)
2383 (setq pos
(string-match "^\\\\hline\n?" output pos
)))
2386 (setq output
(replace-match "" nil nil output
)))))))
2387 (if (not org-e-latex-tables-centered
) output
2388 (format "\\begin{center}\n%s\n\\end{center}" output
))))
2393 (defun org-e-latex-table-cell (table-cell contents info
)
2394 "Transcode a TABLE-CELL element from Org to LaTeX.
2395 CONTENTS is the cell contents. INFO is a plist used as
2396 a communication channel."
2397 (concat (if (and contents
2398 org-e-latex-table-scientific-notation
2399 (string-match orgtbl-exp-regexp contents
))
2400 ;; Use appropriate format string for scientific
2402 (format org-e-latex-table-scientific-notation
2403 (match-string 1 contents
)
2404 (match-string 2 contents
))
2406 (when (org-export-get-next-element table-cell info
) " & ")))
2411 (defun org-e-latex-table-row (table-row contents info
)
2412 "Transcode a TABLE-ROW element from Org to LaTeX.
2413 CONTENTS is the contents of the row. INFO is a plist used as
2414 a communication channel."
2415 ;; Rules are ignored since table separators are deduced from
2416 ;; borders of the current row.
2417 (when (eq (org-element-property :type table-row
) 'standard
)
2418 (let* ((attr (mapconcat 'identity
2419 (org-element-property
2420 :attr_latex
(org-export-get-parent table-row
))
2422 (longtablep (and attr
(string-match "\\<longtable\\>" attr
)))
2424 (or (and attr
(string-match "\\<booktabs=\\(yes\\|t\\)\\>" attr
))
2425 org-e-latex-tables-booktabs
))
2426 ;; TABLE-ROW's borders are extracted from its first cell.
2428 (org-export-table-cell-borders
2429 (car (org-element-contents table-row
)) info
)))
2431 ;; When BOOKTABS are activated enforce top-rule even when no
2432 ;; hline was specifically marked.
2433 (cond ((and booktabsp
(memq 'top borders
)) "\\toprule\n")
2434 ((and (memq 'top borders
) (memq 'above borders
)) "\\hline\n"))
2437 ;; Special case for long tables. Define header and footers.
2438 ((and longtablep
(org-export-table-row-ends-header-p table-row info
))
2441 %s\\multicolumn{%d}{r}{Continued on next page} \\\\
2444 (if booktabsp
"\\midrule" "\\hline")
2445 (if booktabsp
"\\midrule" "\\hline")
2446 ;; Number of columns.
2447 (cdr (org-export-table-dimensions
2448 (org-export-get-parent-table table-row
) info
))))
2449 ;; When BOOKTABS are activated enforce bottom rule even when
2450 ;; no hline was specifically marked.
2451 ((and booktabsp
(memq 'bottom borders
)) "\\bottomrule")
2452 ((and (memq 'bottom borders
) (memq 'below borders
)) "\\hline")
2453 ((memq 'below borders
) (if booktabsp
"\\midrule" "\\hline")))))))
2458 (defun org-e-latex-target (target contents info
)
2459 "Transcode a TARGET object from Org to LaTeX.
2460 CONTENTS is nil. INFO is a plist holding contextual
2462 (format "\\label{%s}"
2463 (org-export-solidify-link-text (org-element-property :value target
))))
2468 (defun org-e-latex-timestamp (timestamp contents info
)
2469 "Transcode a TIMESTAMP object from Org to LaTeX.
2470 CONTENTS is nil. INFO is a plist holding contextual
2472 (let ((value (org-translate-time (org-element-property :value timestamp
)))
2473 (range-end (org-element-property :range-end timestamp
)))
2474 (case (org-element-property :type timestamp
)
2475 (active (format org-e-latex-active-timestamp-format value
))
2477 (concat (format org-e-latex-active-timestamp-format value
)
2479 (format org-e-latex-active-timestamp-format
2480 (org-translate-time range-end
))))
2481 (inactive (format org-e-latex-inactive-timestamp-format value
))
2483 (concat (format org-e-latex-inactive-timestamp-format value
)
2485 (format org-e-latex-inactive-timestamp-format
2486 (org-translate-time range-end
))))
2487 (otherwise (format org-e-latex-diary-timestamp-format value
)))))
2492 (defun org-e-latex-underline (underline contents info
)
2493 "Transcode UNDERLINE from Org to LaTeX.
2494 CONTENTS is the text with underline markup. INFO is a plist
2495 holding contextual information."
2496 (org-e-latex--text-markup contents
'underline
))
2501 (defun org-e-latex-verbatim (verbatim contents info
)
2502 "Transcode a VERBATIM object from Org to LaTeX.
2503 CONTENTS is nil. INFO is a plist used as a communication
2505 (org-e-latex--text-markup (org-element-property :value verbatim
) 'verbatim
))
2510 (defun org-e-latex-verse-block (verse-block contents info
)
2511 "Transcode a VERSE-BLOCK element from Org to LaTeX.
2512 CONTENTS is verse block contents. INFO is a plist holding
2513 contextual information."
2514 (org-e-latex--wrap-label
2516 ;; In a verse environment, add a line break to each newline
2517 ;; character and change each white space at beginning of a line
2518 ;; into a space of 1 em. Also change each blank line with
2519 ;; a vertical space of 1 em.
2521 (setq contents
(replace-regexp-in-string
2522 "^ *\\\\\\\\$" "\\\\vspace*{1em}"
2523 (replace-regexp-in-string
2524 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" contents
)))
2525 (while (string-match "^[ \t]+" contents
)
2526 (let ((new-str (format "\\hspace*{%dem}"
2527 (length (match-string 0 contents
)))))
2528 (setq contents
(replace-match new-str nil t contents
))))
2529 (format "\\begin{verse}\n%s\\end{verse}" contents
))))
2533 ;;; Interactive functions
2536 (defun org-e-latex-export-as-latex
2537 (&optional subtreep visible-only body-only ext-plist
)
2538 "Export current buffer as a LaTeX buffer.
2540 If narrowing is active in the current buffer, only export its
2543 If a region is active, export that region.
2545 When optional argument SUBTREEP is non-nil, export the sub-tree
2546 at point, extracting information from the headline properties
2549 When optional argument VISIBLE-ONLY is non-nil, don't export
2550 contents of hidden elements.
2552 When optional argument BODY-ONLY is non-nil, only write code
2553 between \"\\begin{document}\" and \"\\end{document}\".
2555 EXT-PLIST, when provided, is a property list with external
2556 parameters overriding Org default settings, but still inferior to
2557 file-local settings.
2559 Export is done in a buffer named \"*Org E-LATEX Export*\", which
2560 will be displayed when `org-export-show-temporary-export-buffer'
2563 (let ((outbuf (org-export-to-buffer
2564 'e-latex
"*Org E-LATEX Export*"
2565 subtreep visible-only body-only ext-plist
)))
2566 (with-current-buffer outbuf
(LaTeX-mode))
2567 (when org-export-show-temporary-export-buffer
2568 (switch-to-buffer-other-window outbuf
))))
2571 (defun org-e-latex-export-to-latex
2572 (&optional subtreep visible-only body-only ext-plist pub-dir
)
2573 "Export current buffer to a LaTeX file.
2575 If narrowing is active in the current buffer, only export its
2578 If a region is active, export that region.
2580 When optional argument SUBTREEP is non-nil, export the sub-tree
2581 at point, extracting information from the headline properties
2584 When optional argument VISIBLE-ONLY is non-nil, don't export
2585 contents of hidden elements.
2587 When optional argument BODY-ONLY is non-nil, only write code
2588 between \"\\begin{document}\" and \"\\end{document}\".
2590 EXT-PLIST, when provided, is a property list with external
2591 parameters overriding Org default settings, but still inferior to
2592 file-local settings.
2594 When optional argument PUB-DIR is set, use it as the publishing
2597 Return output file's name."
2599 (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir
)))
2601 'e-latex outfile subtreep visible-only body-only ext-plist
)))
2604 (defun org-e-latex-export-to-pdf
2605 (&optional subtreep visible-only body-only ext-plist pub-dir
)
2606 "Export current buffer to LaTeX then process through to PDF.
2608 If narrowing is active in the current buffer, only export its
2611 If a region is active, export that region.
2613 When optional argument SUBTREEP is non-nil, export the sub-tree
2614 at point, extracting information from the headline properties
2617 When optional argument VISIBLE-ONLY is non-nil, don't export
2618 contents of hidden elements.
2620 When optional argument BODY-ONLY is non-nil, only write code
2621 between \"\\begin{document}\" and \"\\end{document}\".
2623 EXT-PLIST, when provided, is a property list with external
2624 parameters overriding Org default settings, but still inferior to
2625 file-local settings.
2627 When optional argument PUB-DIR is set, use it as the publishing
2630 Return PDF file's name."
2632 (org-e-latex-compile
2633 (org-e-latex-export-to-latex
2634 subtreep visible-only body-only ext-plist pub-dir
)))
2636 (defun org-e-latex-compile (texfile)
2637 "Compile a TeX file.
2639 TEXFILE is the name of the file being compiled. Processing is
2640 done through the command specified in `org-e-latex-pdf-process'.
2642 Return PDF file name or an error if it couldn't be produced."
2643 (let* ((wconfig (current-window-configuration))
2644 (texfile (file-truename texfile
))
2645 (base (file-name-sans-extension texfile
))
2647 (message (format "Processing LaTeX file %s ..." texfile
))
2651 ;; A function is provided: Apply it.
2652 ((functionp org-e-latex-pdf-process
)
2653 (funcall org-e-latex-pdf-process
(shell-quote-argument texfile
)))
2654 ;; A list is provided: Replace %b, %f and %o with appropriate
2655 ;; values in each command before applying it. Output is
2656 ;; redirected to "*Org PDF LaTeX Output*" buffer.
2657 ((consp org-e-latex-pdf-process
)
2658 (let* ((out-dir (or (file-name-directory texfile
) "./"))
2659 (outbuf (get-buffer-create "*Org PDF LaTeX Output*")))
2663 (replace-regexp-in-string
2664 "%b" (shell-quote-argument base
)
2665 (replace-regexp-in-string
2666 "%f" (shell-quote-argument texfile
)
2667 (replace-regexp-in-string
2668 "%o" (shell-quote-argument out-dir
) command t t
) t t
) t t
)
2670 org-e-latex-pdf-process
)
2671 ;; Collect standard errors from output buffer.
2672 (setq errors
(org-e-latex--collect-errors outbuf
))))
2673 (t (error "No valid command to process to PDF")))
2674 (let ((pdffile (concat base
".pdf")))
2675 ;; Check for process failure. Provide collected errors if
2677 (if (not (file-exists-p pdffile
))
2678 (error (concat (format "PDF file %s wasn't produced" pdffile
)
2679 (when errors
(concat ": " errors
))))
2680 ;; Else remove log files, when specified, and signal end of
2681 ;; process to user, along with any error encountered.
2682 (when org-e-latex-remove-logfiles
2683 (dolist (ext org-e-latex-logfiles-extensions
)
2684 (let ((file (concat base
"." ext
)))
2685 (when (file-exists-p file
) (delete-file file
)))))
2686 (message (concat "Process completed"
2687 (if (not errors
) "."
2688 (concat " with errors: " errors
)))))
2689 ;; Return output file name.
2691 (set-window-configuration wconfig
))))
2693 (defun org-e-latex--collect-errors (buffer)
2694 "Collect some kind of errors from \"pdflatex\" command output.
2696 BUFFER is the buffer containing output.
2698 Return collected error types as a string, or nil if there was
2700 (with-current-buffer buffer
2702 (goto-char (point-max))
2703 ;; Find final "pdflatex" run.
2704 (when (re-search-backward "^[ \t]*This is pdf.*?TeX.*?Version" nil t
)
2705 (let ((case-fold-search t
)
2707 (when (save-excursion
2708 (re-search-forward "Reference.*?undefined" nil t
))
2709 (setq errors
(concat errors
" [undefined reference]")))
2710 (when (save-excursion
2711 (re-search-forward "Citation.*?undefined" nil t
))
2712 (setq errors
(concat errors
" [undefined citation]")))
2713 (when (save-excursion
2714 (re-search-forward "Undefined control sequence" nil t
))
2715 (setq errors
(concat errors
" [undefined control sequence]")))
2716 (when (save-excursion
2717 (re-search-forward "^! LaTeX.*?Error" nil t
))
2718 (setq errors
(concat errors
" [LaTeX error]")))
2719 (when (save-excursion
2720 (re-search-forward "^! Package.*?Error" nil t
))
2721 (setq errors
(concat errors
" [package error]")))
2722 (and (org-string-nw-p errors
) (org-trim errors
)))))))
2725 (provide 'org-e-latex
)
2726 ;;; org-e-latex.el ends here